//Main void main(void) { initBoard(); backlightOn(); //Configure interrupts //Interrupt on RB1 = SW_E OpenRB1INT(PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_OFF & PORTB_INT_PRIO_HIGH); //Interrupt on RB0 = SW_W OpenRB0INT(PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_OFF & PORTB_INT_PRIO_LOW); //Enable button input => not needed, see dwengoBoard.c->initBoard(); TRISB = 0xFF; //Init motors initializeMotors(); initializeSensors(); while (TRUE) { if(SW_N == 0) { mode = 0; clearLCD(); printStringToLCD("Doing moves brah", 1, 0); leftMotor(700); rightMotor(700); delay_s(4); leftMotor(1000); rightMotor(700); delay_s(4); leftMotor(700); rightMotor(1000); delay_s(4); leftMotor(-700); rightMotor(700); delay_s(4); } else if(mode == 1) { //Light eating } else if(SW_S == 0) { //Start light eating //mode = 1; clearLCD(); printStringToLCD("Going to send string", 0, 0); initializeRS232(); sendData(); } else { //printStringToLCD("Cool story bro", 0, 0); } } }
void External_Init(void) { INTCON = 0x80; OpenRB0INT( FALLING_EDGE_INT & PORTB_PULLUPS_OFF & PORTB_INT_PRIO_HIGH ); OpenRB1INT( FALLING_EDGE_INT & PORTB_PULLUPS_OFF & PORTB_INT_PRIO_HIGH ); INTCONbits.INT0IE = 1; INTCON3bits.INT1IE = 1; INTCONbits.INT0IF =0; INTCON3bits.INT1IF =0; }
void low_isr(void) { // Réception CAN. if(PIE3bits.RXB0IE && PIR3bits.RXB0IF) { unsigned long id; char num; short cmd; BYTE data[8]; BYTE len; enum CAN_RX_MSG_FLAGS flags; while(CANIsRxReady()) { CANReceiveMessage(&id, data, &len, &flags); } PIR3bits.RXB0IF = 0; led = led ^ 1; num = id & 0x07; cmd = id & 0xFFF8; if(cmd == 320) { // rangerReq id = 352 | (rangers[num].value < rangers[num].threshold) << 4 | num; while(!CANSendMessage(id, (BYTE*)rangers[num].value, 2, CAN_TX_PRIORITY_0 & CAN_TX_STD_FRAME & CAN_TX_NO_RTR_FRAME )) { } } else if(cmd == 328) { // rangerThres rangers[num].threshold = ((unsigned int*) data)[0]; } else if(cmd == 336) { // rangerMute rangers[num].unmuted = 0; } else if(cmd == 344) { // rangerUnmute rangers[num].unmuted = 1; } else { led = led ^ 1; // On annule la commutation précédente de la LED. } } // Génération des pulses sonars et polling des bumpers. if(INTCONbits.TMR0IE && INTCONbits.TMR0IF) // Vingt fois par secondes, non stop. { INTCONbits.TMR0IF = 0; check_button(0, PORTCbits.RC2); check_button(1, PORTCbits.RC3); check_button(2, PORTCbits.RC4); check_button(3, PORTCbits.RC5); // On alterne les triggers pulse, et pas besoin // d'attendre plus car ils restent allumés la moitié du temps (50ms). if(PORTCbits.RC7) { CloseRB1INT(); OpenRB0INT(PORTB_CHANGE_INT_ON & RISING_EDGE_INT & PORTB_PULLUPS_OFF); PORTCbits.RC6 = 1; PORTCbits.RC7 = 0; // Fin du pulse => déclenchement. } else { CloseRB0INT(); OpenRB1INT(PORTB_CHANGE_INT_ON & RISING_EDGE_INT & PORTB_PULLUPS_OFF); PORTCbits.RC6 = 0; PORTCbits.RC7 = 1; // Fin du pulse => déclenchement. } // Début de l'attente des echos. // Lecture de l'ADC pour les sharps et passage à la mesure suivante. //count_sharp_value = (count_sharp_value + 1) %3; //count_sharp_value_tab = count_sharp_value + 3*(cur_sharp - 2); // On se place au bon endroit dans le tableau sharp_value[]. count_sharp_value_tab = count_sharp_value + 3*(cur_sharp - 2); count_sharp_value_change = (count_sharp_value_change + 1) %3; if (count_sharp_value_change == 2) count_sharp_value = (count_sharp_value + 1)%3; count_sharp_value_change = (count_sharp_value_change + 1) %3; sharp_value[count_sharp_value_tab] = LectureAnalogique(); rangers[cur_sharp].value = (sharp_value[3*(cur_sharp - 2)] + sharp_value[1 + 3*(cur_sharp - 2)] + sharp_value[2 + 3*(cur_sharp - 2)])/3; //rangers[cur_sharp].value = LectureAnalogique(); if(rangers[cur_sharp].unmuted) { while(!CANSendMessage(352 | cur_sharp, (BYTE*)&(rangers[cur_sharp].value), 2, CAN_TX_PRIORITY_0 & CAN_TX_STD_FRAME & CAN_TX_NO_RTR_FRAME )) { } led = led ^ 1; } if(++cur_sharp > 4) cur_sharp = 2; // Lancement de la conversion suivante. ADCON0 = 0b00000001 + ((cur_sharp - 2) << 2); // Selectionne le bon AN en lecture analogique //ADCON2 peut être configuré si on le souhaite ADCON0bits.GO_DONE=1; //ADCON0bits.CHS = cur_sharp; // Plus simple que SetChanADC(). //TODO //ConvertADC(); //TODO } }