//------------------------------------------------------------------------------ u08 AVCLan_Read_Byte(u08 length) { u08 byte = 0; u08 wT; while (1) { while (INPUT_IS_CLEAR); timer0_start(); while (INPUT_IS_SET); wT = TCNT0; if (wT<8) { byte++; parity_bit++; } length--; if (!length) return byte; byte = byte << 1; } }
//------------------------------------------------------------------------------ u08 AVCLan_Send_ACK() { timer0_source(CK64); //update every 1us timer0_start(); while (INPUT_IS_CLEAR) { if (TCNT0 >= 25) return 0; // max wait time } AVC_OUT_EN(); AVC_SET_1(); delay1(32); //28-37 AVC_SET_0(); delay1(4); //00-09 AVC_OUT_DIS(); return 1; }
//------------------------------------------------------------------------------ void AVC_HoldLine() { STOPEvent; // wait for free line u08 T=0; u08 line_busy = 1; timer0_source(CK64); timer0_start(); do { while (INPUT_IS_CLEAR) { T = TCNT0; if (T >= 25) break; } if (T > 24) line_busy=0; } while (line_busy); // switch to out mode AVC_OUT_EN(); AVC_SET_1(); STARTEvent; }
//------------------------------------------------------------------------------ u08 AVCLan_SendData() { u08 i; STOPEvent; // wait for free line u08 T=0; u08 line_busy = 1; timer0_source(CK64); timer0_start(); do { while (INPUT_IS_CLEAR) { T = TCNT0; if (T >= 25) break; } if (T > 24) line_busy=0; } while (line_busy); // switch to output mode AVC_OUT_EN(); AVCLan_Send_StartBit(); AVCLan_Send_Byte(0x1, 1); // regular communication parity_bit = 0; AVCLan_Send_Byte(CD_ID_1, 4); // CD Changer ID as master AVCLan_Send_Byte(CD_ID_2, 8); AVCLan_Send_ParityBit(); AVCLan_Send_Byte(HU_ID_1, 4); // HeadUnit ID as slave AVCLan_Send_Byte(HU_ID_2, 8); AVCLan_Send_ParityBit(); if (AVCLan_Read_ACK()) { AVC_OUT_DIS(); STARTEvent; RS232_Print("E1\n"); return 1; } AVCLan_Send_Byte(0xF, 4); // 0xf - control -> COMMAND WRITE AVCLan_Send_ParityBit(); if (AVCLan_Read_ACK()) { AVC_OUT_DIS(); STARTEvent; RS232_Print("E2\n"); return 2; } AVCLan_Send_Byte(data_len, 8);// data lenght AVCLan_Send_ParityBit(); if (AVCLan_Read_ACK()) { AVC_OUT_DIS(); STARTEvent; RS232_Print("E3\n"); return 3; } for (i=0;i<data_len;i++) { AVCLan_Send_Byte(data[i], 8);// data byte AVCLan_Send_ParityBit(); if (AVCLan_Read_ACK()) { AVC_OUT_DIS(); STARTEvent; RS232_Print("E4("); RS232_PrintDec(i); RS232_Print(")\n"); return 4; } } // back to read mode AVC_OUT_DIS(); STARTEvent; if (showLog) ShowOutMessage(); return 0; }
//------------------------------------------------------------------------------ u08 AVCLan_Read_Message() { STOPEvent; // disable timer1 interrupt u08 T = 0; u08 i; u08 for_me = 0; //RS232_Print("$ "); timer0_source(CK64); // check start bit timer0_start(); while (INPUT_IS_SET) { T=TCNT0; if (T>254) { STARTEvent; RS232_Print("LAN>T1\n"); return 0; } } if (T<10) { // !!!!!!! 20 !!!!!!!!!!! STARTEvent; RS232_Print("LAN>T2\n"); return 0; } broadcast = AVCLan_Read_Byte(1); parity_bit = 0; master1 = AVCLan_Read_Byte(4); master2 = AVCLan_Read_Byte(8); if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { STARTEvent; return 0; } parity_bit = 0; slave1 = AVCLan_Read_Byte(4); slave2 = AVCLan_Read_Byte(8); if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { STARTEvent; return 0; } // is this command for me ? if ((slave1==CD_ID_1)&&(slave2==CD_ID_2)) { for_me=1; } if (for_me) AVCLan_Send_ACK(); else AVCLan_Read_Byte(1); parity_bit = 0; AVCLan_Read_Byte(4); // control - always 0xF if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { STARTEvent; return 0; } if (for_me) AVCLan_Send_ACK(); else AVCLan_Read_Byte(1); parity_bit = 0; message_len = AVCLan_Read_Byte(8); if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { STARTEvent; return 0; } if (for_me) AVCLan_Send_ACK(); else AVCLan_Read_Byte(1); if (message_len > MAXMSGLEN) { // RS232_Print("LAN> Command error"); STARTEvent; return 0; } for (i=0; i<message_len; i++) { parity_bit = 0; message[i] = AVCLan_Read_Byte(8); if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { STARTEvent; return 0; } if (for_me) { AVCLan_Send_ACK(); } else { AVCLan_Read_Byte(1); } } STARTEvent; if (showLog) ShowInMessage(); if (for_me) { if (CheckCmd((u08*)stat1)) { answerReq = cmStatus1; return 1; } if (CheckCmd((u08*)stat2)) { answerReq = cmStatus2; return 1; } if (CheckCmd((u08*)stat3)) { answerReq = cmStatus3; return 1; } if (CheckCmd((u08*)stat4)) { answerReq = cmStatus4; return 1; } // if (CheckCmd((u08*)stat5)) { answerReq = cmStatus5; return 1; } if (CheckCmd((u08*)play_req1)) { answerReq = cmPlayReq1; return 1; } if (CheckCmd((u08*)play_req2)) { answerReq = cmPlayReq2; return 1; } if (CheckCmd((u08*)play_req3)) { answerReq = cmPlayReq3; return 1; } if (CheckCmd((u08*)stop_req)) { answerReq = cmStopReq; return 1; } if (CheckCmd((u08*)stop_req2)) { answerReq = cmStopReq2; return 1; } } else { // broadcast check if (CheckCmd((u08*)lan_playit)) { answerReq = cmPlayIt; return 1; } if (CheckCmd((u08*)lan_check)) { answerReq = cmCheck; CMD_CHECK[6]=message[3]; return 1; } if (CheckCmd((u08*)lan_reg)) { answerReq = cmRegister; return 1; } if (CheckCmd((u08*)lan_init)) { answerReq = cmInit; return 1; } if (CheckCmd((u08*)lan_stat1)) { answerReq = cmStatus1; return 1; } } answerReq = cmNull; return 1; }
void timer1_isr_100Hz(uint8_t interrupt_nbr) { set_output(OUT1, HIGH); timer0_start(10); }
void main() { char str_type=PRT_DOTLINE; int line_num=4; port_init(); //端口的初始化// PRT_STM_init(); //状态机的初始化// //time0中断时间精度测试 #ifdef TEST_CASE_TIME0 timer0_start(); while(1); #endif //发送一行dotline测例(仅32个dot) #ifdef TEST_CASE_DOTLINE UART_Data[0]=0x55; UART_Data[1]=0x00; UART_Data[2]=0xff; UART_Data[3]=0x55; while(1) { PRT_DotLine(UART_Data,4); delay(40); } #endif //MOTOR测试 #ifdef TEST_CASE_MOTOR timer0_start(); while(1) { prt_stm.data_ready=1; delay(500); } #endif //快进测试 #ifdef TEST_CASE_FF while(1) { PRT_dry_run(10); delay(500); } #endif while(1) { if(NO_PAPER!=0) //判断是否有纸// { if(str_type == PRT_DOTLINE) { PRT_DotLine(UART_Data,4); } else if(str_type == PRT_DRYRUN) { PRT_dry_run(line_num); } } else { } } }
int main() { int i; #ifdef FPGA pll_init(); #else bt16_pll_init(); #endif delay(500000); uart_init((96000000/ 460800)); // pa8 puts(pubDate); puts("...fpga bt16 setup ok.......\n"); #ifdef FPGA spi_int(); #endif puts("-----1\n"); system_init(); puts("-----2\n"); timer0_start(); puts("-----3\n"); RF_init(); //----------debug HWI_Install(1, exception_isr, 3) ; //timer0_isr ENABLE_INT(); puts("-----4\n"); thread_init(os_create_thread, os_delete_thread); puts("-----5\n"); sys_timer_init(); puts("-----6\n"); ble_main(); btstack_main(); /* device_manager_init(); */ /*INTALL_HWI(BT_BLE_INT, le_hw_isr, 0); INTALL_HWI(18, le_test_uart_isr, 0);*/ lbuf_init(malloc_buf, sizeof(malloc_buf)*4); /* btstack_v21_main(); */ puts("------------4.0 start run loop-----------\n"); while(1) { int c; //asm("idle"); /*delay(100000);*/ for (i=0; i<PRIORITY_NUM; i++) { if (thread_fun[i]){ thread_fun[i](i); } } c = getchar(); switch(c) { case 'A': puts("user cmd : ADV\n"); ble_set_adv(); break; case 'S': puts("user cmd : SCAN\n"); ble_set_scan(); break; default: break; } /*run_loop_execute();*/ /*printf("k");*/ } return 0; }
static void twi_callback(uint8_t buffer_size, volatile uint8_t input_buffer_length, const volatile uint8_t *input_buffer, uint8_t volatile *output_buffer_length, volatile uint8_t *output_buffer) { uint8_t input; uint8_t command; uint8_t io; if(input_buffer_length < 1) return(build_reply(output_buffer_length, output_buffer, 0, 1, 0, 0)); input = input_buffer[0]; command = input & 0xf8; io = input & 0x07; switch(command) { case(0x00): // short / no-io { switch(io) { case(0x00): // identify { static const uint8_t replystring[] = { 0x4a, 0xfb, 0x06, 0x01, 0x00, 't', '8', '6', '1', 'a' }; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } case(0x01): // 0x02 read ADC { uint8_t value; value = ADCW; if(ADCSRA & _BV(ADSC)) // conversion not ready return(build_reply(output_buffer_length, output_buffer, input, 5, 0, 0)); adc_stop(); uint8_t replystring[2]; replystring[0] = (value & 0xff00) >> 8; replystring[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } case(0x02): // 0x02 DEBUG read timer0 counter { uint8_t value = timer0_get_counter(); return(build_reply(output_buffer_length, output_buffer, input, 0, 1, &value)); } case(0x03): // 0x03 DEBUG read timer1 counter { uint16_t value = pwm_timer1_get_counter(); uint8_t rv[2]; rv[0] = (value & 0xff00) >> 8; rv[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(rv), rv)); } case(0x04): // 0x04 DEBUG read timer1 max { uint16_t value = pwm_timer1_get_max(); uint8_t rv[2]; rv[0] = (value & 0xff00) >> 8; rv[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(rv), rv)); } case(0x05): // 0x05 read timer1 prescaler { uint8_t value = pwm_timer1_status(); return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(value), &value)); } case(0x06): // 0x06 DEBUG read timer0 entry / exit counter values { uint8_t value[2]; value[0] = timer0_debug_1; value[1] = timer0_debug_2; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(value), value)); } case(0x07): // extended command { return(build_reply(output_buffer_length, output_buffer, input, 7, 0, 0)); } default: { return(build_reply(output_buffer_length, output_buffer, input, 7, 0, 0)); } } break; } case(0x10): // 0x10 read counter case(0x20): // 0x20 read / reset counter { if(io >= COUNTER_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint32_t counter = counters_meta[io].counter; if(command == 0x20) counters_meta[io].counter = 0; uint8_t replystring[4]; replystring[0] = (counter & 0xff000000) >> 24; replystring[1] = (counter & 0x00ff0000) >> 16; replystring[2] = (counter & 0x0000ff00) >> 8; replystring[3] = (counter & 0x000000ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } case(0x30): // read input { uint8_t value; if(io >= INPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); value = !!(*input_ports[io].port & (1 << input_ports[io].bit)); return(build_reply(output_buffer_length, output_buffer, input, 0, 1, &value)); } case(0x40): // write output / softpwm { if(input_buffer_length < 2) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); softpwm_meta[io].duty = input_buffer[1]; update_static_softpwm_ports(); timer0_start(); return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(softpwm_meta), (uint8_t *)softpwm_meta)); } case(0x50): // read output / softpwm { if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); duty = softpwm_meta[io].duty; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(duty), &duty)); } case(0x60): // write softpwm mode { if(input_buffer_length < 2) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode = input_buffer[1]; if(mode > 3) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); softpwm_meta[io].pwm_mode = input_buffer[1]; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(softpwm_meta), (uint8_t *)softpwm_meta)); } case(0x70): // read softpwm mode { if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode; mode = softpwm_meta[io].pwm_mode; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(mode), &mode)); } case(0x80): // write pwm { if(input_buffer_length < 3) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint16_t value; value = input_buffer[1]; value <<= 8; value |= input_buffer[2]; pwm_timer1_set_pwm(io, value); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0x90): // read pwm { if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint16_t value = pwm_timer1_get_pwm(io); uint8_t reply[2]; reply[0] = (value & 0xff00) >> 8; reply[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(reply), reply)); } case(0xa0): // write pwm mode { if(input_buffer_length < 2) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode = input_buffer[1]; if(mode > 3) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); pwm_meta[io].pwm_mode = input_buffer[1]; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(pwm_meta), (uint8_t *)pwm_meta)); } case(0xb0): // read pwm mode { if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode; mode = pwm_meta[io].pwm_mode; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(mode), &mode)); } case(0xc0): // start adc conversion { if(io > 1) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); adc_start(io); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0xf0): // twi stats { uint8_t replystring[2]; uint16_t stats; switch(io) { case(0x00): // disable { usi_twi_enable_stats(0); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0x01): // enable { usi_twi_enable_stats(1); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0x02): // read start conditions { stats = usi_twi_stats_start_conditions(); break; } case(0x03): // read stop conditions { stats = usi_twi_stats_stop_conditions(); break; } case(0x04): // read error conditions { stats = usi_twi_stats_error_conditions(); break; } case(0x05): // read overflow conditions { stats = usi_twi_stats_overflow_conditions(); break; } case(0x06): // read local frames { stats = usi_twi_stats_local_frames(); break; } case(0x07): // read idle calls { stats = usi_twi_stats_idle_calls(); break; } } replystring[0] = (stats & 0xff00) >> 8; replystring[1] = (stats & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } default: { return(build_reply(output_buffer_length, output_buffer, input, 2, 0, 0)); } } return(build_reply(output_buffer_length, output_buffer, input, 2, 0, 0)); }
void main (void) { //unsigned char i; unsigned char my_order; //static char count_burst; init_ioport(); spi_init(); rf_init(); //msp_init(); //timer0_init(); adc_init(); timer0_init(); timer0_start(); //regCallbackFunc(); //led_turnOn(); BUF_RDY = 0; rf_config(RX); rf_rxOn(); /* initial blinking */ //for(my_order=0; my_order<=MyID; my_order++) for(my_order=0; my_order<=1; my_order++) { led_turnOn(); delayms(10); led_turnOff(); delayms(1000); } /**/ for(;;) { rf_rxPacket(Buffer); if (Buffer[0] != MyID && Buffer[1] != MyID && Buffer[2] != MyID) { rf_rxOn(); continue; } if(Buffer[0] == MyID) my_order = 0; else if(Buffer[1] == MyID) my_order = 1; else my_order = 2; Buffer[0] = Buffer[1] = Buffer[2] = -1; // xmitdata() uses 'Buffer' to send data --> reset! //PWR_UP = 0; MSP_CS = 1; switch(my_order) { case 0: //_delay2us(150); _delay2us(170); //350 us break; case 1: delayms(1); _delay2us(245); //500 us //_delay2us(185); _delay2us(195); //400 break; case 2: delayms(2); _delay2us(245); // 5oo us //_delay2us(185); _delay2us(205); // 420 us break; } timer0_stop(); MSP_CS = 0; xmitdata(); MSP_CS = 1; led_toggle(); rf_config(RX); rf_rxOn(); timer0_start(); MSP_CS = 0; } // for } // main
int main() { char i = 0x00; ADCTRIS |= ADCPIN; i = 0; SNSTRIS |= LVLONE|LVLTWO; LEDTRIS &= ~(POWLED|ALMLED); ALMTRIS &= ~(LGTOUT|ALMOUT); init_sensor(&levelSensors[0]); init_sensor(&levelSensors[1]); timer0_init(); timer1_init(); timer2_init(); adc_init_CH0(); LEDPORT |= POWLED; ALMPORT &= ~(ALMOUT|LGTOUT); timer0_start(); //Blinking Timer timer2_start(); //Start Timer Counting TODO: Shutoff if Both Sensors dont use while(1) { levelSensors[0].sensorRead = (SNSPORT & LVLONE); levelSensors[1].sensorRead = (SNSPORT & LVLTWO); for(i = 0; i < 2;i++) { checkTankStatus(&levelSensors[i]); checkSensorState(&levelSensors[i]); checkAlarmState(&levelSensors[i], &theAlarm); } // checkTankStatus(&levelSensors[0]); // checkTankStatus(&levelSensors[1]); // // checkSensorState(&levelSensors[0]); // checkSensorState(&levelSensors[1]); // // checkAlarmState(&levelSensors[0], &theAlarm); // checkAlarmState(&levelSensors[1], &theAlarm); blinkLed(&(levelSensors[0].LEVEL_STATE), &(levelSensors[1].LEVEL_STATE), &(theAlarm.ALARM_STATE), &blinkState); } //NOT A GOOD SIGNAL!! ERROR! while(1) { LEDPORT^=POWLED; for(int i=0;i<10000;i++); } return (EXIT_SUCCESS); }