int main(){ first_time_boot(); //first time boot parameters read_config(); //nuskaitom config is eeprom //------------- I/O nustatymai ------------------ //LED PORTDDR(LED_PORT) |= _BV(LED1_BIT); LED_PORT &= ~_BV(LED1_BIT); PORTDDR(LED_PORT) |= _BV(LED2_BIT); LED_PORT &= ~_BV(LED2_BIT); PORTDDR(LED_PORT) |= _BV(LED3_BIT); LED_PORT &= ~_BV(LED3_BIT); PORTDDR(LCD_LED_PORT) |= _BV(LCD_LED_BIT); //LCD pasvietimas LCD_LED_PORT |= _BV(LCD_LED_BIT); //ijungiam LCD pasvietimas //USART DDRD|=_BV(PD1); //TX PORTD|=_BV(PD1); DDRD&=~_BV(PD0); //RX //ROT ENCODER DDRD&=~_BV(PD3); //INT1 PORTD|=_BV(PD3); DDRD&=~_BV(PD4); PORTD|=_BV(PD4); //ROT ENCODER BUTTON DDRB&=~_BV(PB6); PORTB|=_BV(PB6); //PIR DDRD&=~_BV(PD2); //INT0 // PORTD|=_BV(PD2); //PWM OUT DDRB|=_BV(PB3); //----------------- initai ----------------------------- init_uart(UBRR_VAL); lcd_init(LCD_DISP_ON); INT_init(); ADC_init(); timer_init_0(); timer_init_1(); start_timer1(); // TIMSK |=(_BV(OCIE1A)); //iddle timmer on apie(); //puslapiai(); //Nustatom PWM mode // TCCR2=0x6B; //6E; // OCR2=EEPROM_read(24); // OCR2 is EEPROM work_mode=EEPROM_read(25); wmode(work_mode); show_work_mode(); #if debug_mode send_string("OCR2 eeprome: "); send_string(itoa(OCR2, buff, 10)); send_string("\n\r"); #endif //naudojam ADC nuskaitymui // start_timer0(); _delay_ms(200); LCD_LED_PORT &= ~_BV(LCD_LED_BIT); //isjungiam LCD pasvietima sei(); //fade_in(); //fade_out(); while(5){ PORTDDR(LED_PORT)^= (1<<LED3_BIT); // _delay_ms(50); //-------------------- to go into main menu 00-------------------- // if (bit_is_clear(PINB, PB6)){ // lcd_light=1; // } if(meniu==0 && config==0 && read_keypad()==1){ #if debug_mode PORTDDR(LED_PORT)^= (1<<LED2_BIT); send_string("nuspausta knopke\n\r"); #endif if(lcd_light==0) { clock_second=0; clock_millisecond=0; lcd_light=1; LCD_LED_PORT |= _BV(LCD_LED_BIT); //ijungiam LCD pasvietima } else{ LCD_LED_PORT |= _BV(LCD_LED_BIT); //ijungiam LCD pasvietima meniu=1; menu_page=0; sub_menu_page=0; lcd_light=0; puslapiai(); } #if debug_mode debug_meniu(); #endif } //-------------------- main and sub menu routine -------------- while(meniu!=0 && config==0){ if(read_keypad()==1){ if(meniu==1 && config==0){ if(menu_page<4){ //2inis configas (on/off tipo) meniu=2; sub_menu_page=0; puslapiai_2(); #if debug_mode debug_meniu(); #endif } else if (menu_page==4){ //isejimas i configa meniu=3; config=1; read_config(); //nuskaitom config parametrus is eeprom puslapiai_config(); #if debug_mode debug_meniu(); #endif } //exit meniu punktas else if (menu_page==5){ //iseijimas i work_mode(); meniu=0; config=0; show_work_mode(); LCD_LED_PORT &= ~_BV(LCD_LED_BIT); //isjungiam LCD pasvietima #if debug_mode debug_meniu(); #endif } else meniu=meniu; } //-------------------- end of main menu routine -------------- //-------------------- start of sub menu routine -------------- else if (meniu==2 && config==0){ //2inis configas if(sub_menu_page==1) { //OFF vektorius visiems meniu if(menu_page!=4){ meniu=1; work_mode=0; //OFF - wmode EEPROM_write(25, work_mode); wmode(work_mode); puslapiai(); #if debug_mode debug_meniu(); #endif } } else if (sub_menu_page==0){ //ON vektorius if(menu_page==0){ //ON/OFF meniu punkte work_mode=1; //ON - wmode EEPROM_write(25, work_mode); meniu=0; wmode(work_mode); show_work_mode(); _delay_ms(200); LCD_LED_PORT &= ~_BV(LCD_LED_BIT); //isjungiam LCD pasvietima #if debug_mode debug_meniu(); #endif } else if (menu_page==1){ //PIR meniu punktas work_mode=2; //PIR - wmode EEPROM_write(25, work_mode); meniu=0; show_work_mode(); wmode(work_mode); _delay_ms(200); LCD_LED_PORT &= ~_BV(LCD_LED_BIT); //isjungiam LCD pasvietima } else if (menu_page==2){ //PIR/LDR meniu punktas work_mode=3; //PIR/LDR - wmode EEPROM_write(25, work_mode); meniu=0; show_work_mode(); wmode(work_mode); _delay_ms(200); clock_second=0; clock_millisecond=0; lcd_light=1; //LCD_LED_PORT &= ~_BV(LCD_LED_BIT); //isjungiam LCD pasvietima #if debug_mode debug_meniu(); #endif } else if (menu_page==3){ //LDR meniu punktas work_mode=4; //LDR - wmode EEPROM_write(25, work_mode); meniu=0; show_work_mode(); wmode(work_mode); _delay_ms(200); LCD_LED_PORT &= ~_BV(LCD_LED_BIT); //isjungiam LCD pasvietima #if debug_mode debug_meniu(); #endif } } else meniu=meniu; } } } //-------------------- end of sub menu routine -------------- //-------------------------- config --------------------- while(meniu!=0 && config==1){ if(read_keypad()==1){ #if debug_mode PORTDDR(LED_PORT)^= (1<<LED1_BIT); send_string("nuspausta knopke confige\n\r"); #endif //config meniu vaiksciojimas: arba iseinam i main meniu arba nueinam i sub config meniu if(meniu==3 && config==1){ //exit meniu punktas if (config_menu_page==config_menu_page_max){ //iseijimas i work_mode(); meniu=1; config=0; config_menu_page=0; puslapiai(); #if debug_mode debug_meniu(); #endif } //nuejimas i sub config meniu (minPWM, maxPWM, timeOUT ir LDRth nustatymai) else if (config_menu_page!=config_menu_page_max){ meniu=3; config=2; //nuostato nustatymas (pasiziurim, kelintas fadein[] elementas atinka minPWM/maxPWM reiksme EEProme if (config_menu_page==0) nuostatas=nuostato_radimas(minPWM); else if (config_menu_page==1) nuostatas=nuostato_radimas(maxPWM); else nuostatas=nuostatas; puslapiai_config_2(); #if debug_mode debug_meniu(); #endif // break; } } else config=config; } } //---------------------- end of config ----------------------- //---------------------- sub config routine ------------------------- while(meniu!=0 && config==2){ if(read_keypad()==1){ #if debug_mode PORTDDR(LED_PORT)^= (1<<LED1_BIT); send_string("nuspausta knopke sub confige\n\r"); #endif //isejimas is sub configo meniu=3; config=1; nuostatas=0; puslapiai_config(); write_config(); //OCR2=EEPROM_read(24); timer2_set(EEPROM_read(24)); wmode(work_mode); //kad liktu tikrasis work_mode } else config=config; } //---------------------- end of sub config routine ------------------ } }
int main (void) { char x, y, temp, q; ioinit(); //Setup IO pins and defaults //USART_Init( MYUBRR); set_baud(6);//115200 rprintf_devopen(put_char); /* init rrprintf */ //check for existing preset values============================================================== temp = EEPROM_read((unsigned int)BPS); if ((temp < 1) | (temp > 6))//BPS will only be 1-6 { cli();//Disable Interrupts EEPROM_write((unsigned int) BPS, 6); EEPROM_write((unsigned int) BACKLIGHT, 100); EEPROM_write((unsigned int) SPLASH, 1); EEPROM_write((unsigned int) REV, 0); sei();//Enable Interrupts BL_dutycycle = 100; baud_rate = 6; splash_screen = 1; reverse = 0; } else { baud_rate = temp; BL_dutycycle = EEPROM_read((unsigned int)BACKLIGHT); splash_screen = EEPROM_read((unsigned int)SPLASH); reverse = EEPROM_read((unsigned int)REV); } //Reset the display PORTC &= ~(1 << RESET); delay_ms(50); PORTC |= (1 << RESET); //delay_ms(500); clear_screen(); set_page(0); set_x(0); display_on(); //set display start line to 0 //set control lines PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down set_data(0xC0); //set_data(0xFF); delay(); PORTC |= (1 << EN);//up delay(); PORTC &= ~(1 << EN);//down delay(); PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high delay(); x_offset = 0; set_page(0); DDRB |= (1<<BL_EN);//set PB2 as output set_backlight(BL_dutycycle); //Logo========================================================== if (splash_screen == 1) { y = 40; for (q = 0; q < 30; q++) { temp = logo[q]; for (x = 56; x < 64; x++) { if (temp & 0x80) pixel(1,x,y); temp <<= 1; } q++; temp = logo[q]; for (x = 64; x < 72; x++) { if (temp & 0x80) pixel(1,x,y); temp <<= 1; } y--; } } pixel(0,0,0);//cheat RX_in = 0; delay_ms(1000); clear_screen(); if (RX_in > 0)//revert to 115200 { print_char(1,'1'); print_char(1,'1'); print_char(1,'5'); print_char(1,'2'); print_char(1,'0'); print_char(1,'0'); baud_rate = 6; set_baud(6);//115200 cli(); EEPROM_write((unsigned int) BPS, 6); sei();//Enable Interrupts } else (set_baud(baud_rate)); delay_ms(1000); clear_screen(); //main loop=================================================== while(1) { if(RX_in != RX_read) { x = RX_array[RX_read]; RX_read++; if(RX_read >= 416) RX_read = 0; //Backspace=================================================== if(x == 8) del_char(0); //Special commands else if (x == 124) { //make sure the next byte is there while(RX_in == RX_read); //0, clear screen====================================================== if(RX_array[RX_read] == 0)//^@ { clear_screen(); RX_read++; if(RX_read >= 416) RX_read = 0; } //demo mode else if(RX_array[RX_read] == 4)//^d { RX_in = 0, RX_read = 0; demo(); clear_screen(); RX_in = 0; } //reverse mode else if(RX_array[RX_read] == 18)//^r { reverse ^= 1; clear_screen(); RX_read++; if(RX_read >= 416) RX_read = 0; cli(); EEPROM_write((unsigned int) REV, reverse); sei(); } //toggle spasl screen else if(RX_array[RX_read] == 19)//^s { splash_screen ^= 1; //clear_screen(); RX_read++; if(RX_read >= 416) RX_read = 0; cli(); EEPROM_write((unsigned int) SPLASH, splash_screen); sei(); } else { //set backlight (0 to 100)========================================================= if(RX_array[RX_read] == 2)//^b { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte BL_dutycycle = RX_array[RX_read]; RX_read++; if(RX_read >= 416) RX_read = 0; set_backlight(BL_dutycycle); cli(); EEPROM_write((unsigned int) BACKLIGHT, BL_dutycycle); sei(); } //change baud rate========================================================= if(RX_array[RX_read] == 7)//^g { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte //if (RX_array[RX_read] == '1') USART_Init( 1000000/2400-1);//4800 //else if (RX_array[RX_read] == '2') USART_Init( 1000000/4800-1);//9600 //else if (RX_array[RX_read] == '3') USART_Init( 1000000/9600-1);//19200 //else if (RX_array[RX_read] == '4') USART_Init( 1000000/19200-1);//38400 //else if (RX_array[RX_read] == '5') USART_Init( 1000000/28800-1);//57600 //else if (RX_array[RX_read] == '6') USART_Init( 1000000/57600-1);//115200 if ((RX_array[RX_read] > '0') * (RX_array[RX_read] < '7')) baud_rate = (RX_array[RX_read]) - 48; set_baud(baud_rate); cli(); EEPROM_write((unsigned int) BPS, baud_rate); sei(); RX_read++; if(RX_read >= 416) RX_read = 0; } //set x or y========================================================= if((RX_array[RX_read] == 24) | (RX_array[RX_read] == 25))//^x or ^y { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte if (RX_array[RX_read-1] == 24) x_offset = RX_array[RX_read]; else if (RX_array[RX_read-1] == 25) y_offset = RX_array[RX_read]; RX_read++; if(RX_read >= 416) RX_read = 0; if (x_offset > 159) x_offset = 159; if (y_offset > 127) y_offset = 127; } //set pixel========================================================= if (RX_array[RX_read] == 16)//^p { //need 3 bytes for (y = 0; y < 3; y++) { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte } pixel(RX_array[RX_read], RX_array[RX_read-2], RX_array[RX_read-1]); RX_read++; if(RX_read >= 416) RX_read = 0; } //<ctrl>c, circle====================================================== if(RX_array[RX_read] == 3)//^c { //need 4 bytes for (y = 0; y < 4; y++) { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte } circle(RX_array[RX_read], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1]); RX_read++; if(RX_read >= 416) RX_read = 0; } //<ctrl>e, erase block====================================================== if(RX_array[RX_read] == 5)//^e { //need 4 bytes for (y = 0; y < 4; y++) { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte } erase_block(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); RX_read++; if(RX_read >= 416) RX_read = 0; } //box====================================================== if(RX_array[RX_read] == 15)//^o { //need 4 bytes for (y = 0; y < 4; y++) { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte } box(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); RX_read++; if(RX_read >= 416) RX_read = 0; } //line======================================================== else if (RX_array[RX_read] == 12)//^l { //need 5 bytes for (y = 0; y < 5; y++) { RX_read++; if(RX_read >= 416) RX_read = 0; while(RX_in == RX_read);//wait for byte } line(RX_array[RX_read], RX_array[RX_read-4], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read+-1]); RX_read++; if(RX_read >= 416) RX_read = 0; } } } //print character to the screen=============================================== else { del_char(1); print_char(1, x); } } } //demo(); }
void EEPROM_read_block( uint8_t addr, uint8_t *data, uint8_t size ) { while( size-- ) *data++ = EEPROM_read( addr++ ); }
void EEPROM_read_buf(uintptr_t address, void *buf, size_t len) { for (uintptr_t i = 0; i < len; ++i) { ((uint8_t *)buf)[i] = EEPROM_read(address + i); } }
void main(void) { unsigned int position=0; int error=0,perror=0; long int iterm=0; //Declaration related to PID control systems. unsigned char com_data[7]; unsigned int servo_output=1500; int correction; unsigned char lap=0; unsigned int totalsum; int start_count=0; //variable to be removed... // DC pid control variables static int error_dc,perror_dc=0; //error can be +ve or -ve int correction_dc=0; //+ve or -ve unsigned char speed_dc = 40; unsigned char count=0; Leds_and_Switches_Init(); EEPROM_Init(); //EEPROM_reset(); EEPROM_read(); dc_motor_init(); // Current pwm duty is 85 ISR_init(); ATD_init(); pulse_counter_init(); servo_init(); //uart0_9600_init(); //To be removed in the final version of the software. TI1_Enable(); while(1) { //This function compensates the sen_data array and puts the result into com_data array. compensate_value(sen_data,com_data); // Placing it here, will increase the lag between value read and action taken.. hence // making the system unresponsive ... ATD0CTL5_SCAN=0; //This will start a new conversion .... // CODE FOR DC_PID CONTROL.. if(dc_newval_flag) { dc_newval_flag=0; perror_dc=error_dc; error_dc = setpoint - pulse_count; correction_dc =(int) (((KP_DC*error_dc )/DIV10)+ (KD_DC*(error_dc-perror_dc)/DIV10)); if(speed_dc + correction_dc>=200) { speed_dc=200; } else if(speed_dc + correction_dc <=0) { speed_dc =0; } else { // A type of integrator speed_dc +=correction_dc; } dc_motor_speed(speed_dc); } //START STOP DETEECTION //119 15 116 221 134 52 189 totalsum = com_data[4]+com_data[5]+com_data[6]+com_data[0]+com_data[1]+com_data[2]+com_data[3]; if(totalsum>START_OR_CROSS && totalsum<SURE_START) //either CROSS or start ... { //SUM OF SENSORS IS LARGE OS EITHER A CROSS OR START.. //LED1=LED_ON ; if(start_count>delay_dc) { if(lap>=2) { dc_motor_stop(); //TSCR1_TEN = 0; // Stop timer //dc_newval_flag=0; } else { lap++; } } } // CAN BE COMMENTED IN FINAL VERSION OF CODE else if(totalsum<ALL_WHITE) { //LED2=LED_ON ; //do nothing.. } else { //LED3=LED_ON ; start_count=0; //for start/cross detection sort_array(com_data); /*To find the correction, we have three branches .... */ //GUARD 1 if(index ==0 && com_data[index]<GUARD_VAL) { //LEFT //maore the 1500 correction =MAX_CORRECTION; //LED4=LED_ON; } //GUARD 2 else if(index==6 && com_data[index]<GUARD_VAL) { //RIGHT // less than 1500 correction =MIN_CORRECTION; //LED4=LED_ON; } else { //LED3=LED_OFF; position = (index << 9) + (com_data[index+1]-com_data[index-1]) ; perror=error; //stores previous error error = 1536 - position; //PID correction = (int)((int)((kp*error)/DIV100) + (int)((ki *iterm)/DIV1000) + (int)(kd*(error-perror)/DIV1000)); //anti wind up circutary if (correction >= MAX_CORRECTION) { correction = MAX_CORRECTION; } else if (correction <= MIN_CORRECTION) { correction = MIN_CORRECTION; } else { // limits bound on iterm. if (iterm>=ITERM_LIMIT) { //To buffer the fast responses of propotional control, we have the integral control. iterm = ITERM_LIMIT; //LED4=~LED4; } else if (iterm<=-ITERM_LIMIT) { //To buffer the fast responses of propotional control, we have the integral control. iterm = -ITERM_LIMIT; //LED4=~LED4; } else { iterm += error/20; } } } /* PID of dc motor should be applied based on the result of the pid of SERVO ie ""correction"" As the frequency of action of DC PID is slow,, 65 ms, so the lag is enough to drive the car out of track.. DIRECT ACCESS TO DC MOTOR SHOULD BE MADE .. while settiing thr """setpoint""" */ // correction will be handled from the guards also... if(correction > SHARP_TURN || (-correction) > SHARP_TURN) { //dc_motor_speed(10); setpoint=2; } else if ((correction>MIDDLE_TURN && correction <SHARP_TURN) || (correction < -MIDDLE_TURN && correction >-SHARP_TURN)) { //dc_motor_speed(20); setpoint=3; } else { //dc_motor_speed(50); setpoint =4; } servo_output = (unsigned int)(SET_POINT-correction); servo_set(servo_output); } } }