/* *------------------------------------------------------------------------------ * void InitializeUART(void) * * Summary : Initialize Uart for communication with PC * char conunters * Transimission and Reception interrupts are enabled * * Refer PIC 18F4520 datasheet Table 18-3. * * Input : None * * Output : None *------------------------------------------------------------------------------ */ void UART1_init(void) { UINT8 uartConfig= USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH; // configure USART #ifdef ANDONTERMINAL_V0 Open1USART( uartConfig , // 25); //baurdrate for 19200 51); //baurdrate for 19200 #endif #ifdef ANDONTERMINAL_V2 Open1USART( uartConfig , //51); //baurdrate for 19200 15); // baurdrate for 62500 #endif // Enable interrupt priority // RCONbits.IPEN = 1; TXSTAbits.TXEN = 1; //enable transmission TXSTAbits.CSRC = 0; PIE1bits.RC1IE = 1; IPR1bits.TXIP = 1; //make transmit interrupt high priority }
void UART_init(){ // PIN A0 is temp[0]In, A1 is bp1In, A2 is bp2In, A3 is pulseIn //C6 is USART/RS232 Tx, C7 is USART/RS232 Rx TRISCbits.RC6 = 0; //TX pin set as output TRISCbits.RC7 = 1; //RX pin set as input // Enable digital for all c pins ANSELC=0x00; //LATC =0x00; //TRISC =0x00; RCONbits.IPEN = 1; // turn on priorities IPR1bits.RCIP = 1; // make Rx interrupt high priority INTCONbits.GIEH = 1; // turn on high priority interrupts Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & //USART_CONT_RX & USART_BRGH_HIGH, BAUD_9600_4MHz); //Set Baud Rate here Fosc / (64 * (spbrg + 1)) Write1USART(0x0c); // clear hyperterminal }
/* *------------------------------------------------------------------------------ * void InitializeUART(void) * * Summary : Initialize Uart for communication with PC * char conunters * Transimission and Reception interrupts are enabled * * Refer PIC 18F4520 datasheet Table 18-3. * * Input : None * * Output : None *------------------------------------------------------------------------------ */ void UART_init(void) { UINT8 uartConfig= USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH; // configure USART Open1USART( uartConfig , //77); //129 ); //10 ); //19 ); 15 ); // Enable interrupt priority RCONbits.IPEN = 1; TXSTAbits.TXEN = 1; //enable transmission TXSTAbits.CSRC = 0; // IPR1bits.TXIP = 1; //make transmit interrupt high priority INTCONbits.PEIE = 1; //enable low priority interrupts // Make receive interrupt high priority IPR1bits.RCIP = 1; }
void Setup_USART1XBAPI() { //Declaración de Variables unsigned char USART1Config=0; unsigned int BaudRate=0; TRISCbits.RC6=0; //TX es salida TRISCbits.RC7=1; //RX es entrada //Configuración USART USART1Config=USART_TX_INT_OFF //Interrupcion por Ttransmisión: Off &USART_RX_INT_OFF //Interrupcion por Recepción: Off &USART_ASYNCH_MODE //Modo Asíncrono &USART_EIGHT_BIT //Transmision de 8bits &USART_CONT_RX //Recepción Continua &USART_BRGH_HIGH //Baudios &USART_ADDEN_OFF; //Detección de Dirección OFF BaudRate=51; //Valor que se carga a SPRBH para Definir BaudRate = 9600 Close1USART(); //Cierra USART2 en caso de estar previamente abierto. Open1USART(USART1Config, BaudRate); //Abre USART1 IPR1bits.RC1IP =1; //Receive Interrupt: High Priority PIE1bits.RC1IE=1; //Receive Interrupt: Enabled PIR1bits.RC1IF=0; //Reset de EUSART2 Receive Interrupt Flag }
/* *------------------------------------------------------------------------------ * void InitializeUART(void) * * Summary : Initialize Uart for communication with PC * char conunters * Transimission and Reception interrupts are enabled * * Refer PIC 18F4520 datasheet Table 18-3. * * Input : None * * Output : None *------------------------------------------------------------------------------ */ void UART1_init(unsigned long baudRate) { UINT8 uartConfig= USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH; baudRate += (unsigned long)1; baudRate *= (unsigned long)16; baudRate = GetSystemClock() / baudRate; // configure USART Open1USART( uartConfig , baudRate); // Enable interrupt priority // RCONbits.IPEN = 1; RCSTA1bits.SPEN = 1; TXSTA1bits.TXEN = 1; //enable transmission TXSTA1bits.CSRC = 0; PIE1bits.RC1IE = 1; // IPR1bits.TXIP = 1; //make transmit interrupt high priority // Make receive interrupt high priority IPR1bits.RC1IP = 1; }
/** * Configure the processor */ void config_proc() { // Configure the oscillator to use the internal 8MHz crystal OSCCONbits.IRCF0 = 1; OSCCONbits.IRCF1 = 1; OSCCONbits.IRCF2 = 1; // Configure RA0-4 and RB0-4 as digital pins and not analog pins ANCON0 = ANCON0|0x1F; ANCON1 = ANCON1|0x1F; TRISAbits.TRISA0 = 1; // Input for the range_0 selector TRISAbits.TRISA1 = 1; // Input for the range_1 selector TRISAbits.TRISA2 = 1; // Input for the selftest switch ADXL345_ALT_TRIS = 0; // Output for I2C address selection ADXL345_INT1_TRIS = 1; // Input for Accelerometer interrupt 1 ADXL345_INT2_TRIS = 1; // Input for Accelerometer interrupt 2 ADXL345_CS_TRIS = 0; // Output for Accelerometer CS line TRISCbits.TRISC1 = 0; // Output for the status LED // Initially turn off the LED LATCbits.LATC1 = 0; // Set some of the UART registers BAUDCON1bits.BRG16 = 0; TXSTA1bits.BRGH = 1; // Configure the USART port Open1USART(USART_TX_INT_OFF&USART_RX_INT_OFF&USART_EIGHT_BIT&USART_ASYNCH_MODE,51); }
/* * INITIALIZATION AND MISC */ void setup_1(void) { uint8_t i; IFI_Initialization(); /* Prevent the "slow loop" from executing until data has been received * from the master processor. */ statusflag.NEW_SPI_DATA = 0; /* Enable autonomous mode. FIXME: Magic Number (we need an enum of valid "user_cmd"s) */ /* txdata.user_cmd = 0x02; */ /* Make the master control all PWMs (for now) */ txdata.pwm_mask.a = 0xFF; /* Initialize all pins as inputs unless overridden. */ for (i = 0; i < 16; ++i) { pin_set_io( i, kInput); } /* Initialize Serial */ Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, kBaud115); Delay1KTCYx( 50 ); /* Settling time (5K Clock ticks) */ /* Init ADC */ /* Setup the number of analog sensors. The PIC defines a series of 15 * ADC constants in mcc18/h/adc.h that are all of the form 0b1111xxxx, * where x counts the number of DIGITAL ports. In total, there are * sixteen ports numbered from 0ANA to 15ANA. */ if ( NUM_ANALOG_VALID(kNumAnalogInputs) && kNumAnalogInputs > 0 ) { /* ADC_FOSC: Based on a baud_115 value of 21, given the formula * FOSC/(16(X + 1)) in table 18-1 of the PIC18F8520 doc the FOSC * is 40Mhz. * Also according to the doc, section 19.2, the * ADC Freq needs to be at least 1.6us or 0.625MHz. 40/0.625=64 * (Also, see table 19-1 in the chip doc) */ OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ( 0xF0 | (16 - kNumAnalogInputs) ) , ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ); } }
/****************************************************************************** * Function: void InitializeUSART(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This routine initializes the UART to 19200 * * Note: * *****************************************************************************/ void InitializeUSART(void) { RPINR16 = 8; // Assign RX2 to Pin 26 / RP8 / RB5 RPOR7 = 6; // Assign TX2 to Pin 25 / RP7 / RB4 EECON2 = 0x55; EECON2 = 0xAA; PPSCONbits.IOLOCK = 1; Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,77); Open2USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,77); }
static void run_all_tests(){ // CONFIGURE UART FOR TESTS OUTPUT Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_BRGH_HIGH & USART_CONT_RX & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, 8); test_suite_1(); test_suite_2(); END_TESTS(); }
/*========================PORT BITS INITIALISATION============================== GLOBAL INTERRUPT I/O PIN CONFIG LCD SET UP ==============================================================================*/ void initPorts(void) { INTERRUPT_Config(); OSCCON = 0b01110010; // internal oscillator, 16MHz OSCTUNEbits.TUN=011111; WriteMemory(53,0); //set init flag to null (first initialisation) DHT_Config(); HCSR_Config(); SOLAR_POWER_CONFIG = 0; SOLAR_SIGNAL_CONFIG = 1; SOLAR_POWER=0; COND_SIGNAL_CONFIG = 1; COND_POWER_CONFIG =0; BATT_SIGNAL_CONFIG = 1; //Setting USART PORT SIM900_POWER_CONFIG = 0; SIM900_TRIGGER_CONFIG = 0; SIM900_TRIGGER = 1; SIM900_TX_CONFIG = 1; SIM900_RX_CONFIG = 1; // Configure UART Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 25); ANSELC = 0x00; //Setting PC USART COM PIC_TX2_CONFIG = 1; PIC_RX2_CONFIG = 1; // Configure UART Open2USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 25); ANSELB = 0x00; show_Welcome(); delay_100ms(5); }
void main (void) { //Asetetaan oikeat alkuarvot //oskillaattorin biteille... IRCF2 = 1; IRCF1 = 1; IRCF0 = 0; //...ja ledille LED_DIR = 0; LED_PWR=0; TRISB = 0; //Avataan USART-vayla //8-1 bit, 9600baud, asych Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 25); //Sallitaan keskeytykset RCONbits.IPEN =1; //Vastaanotetuista keskeytyksista //korkean prioriteetin keskeytyksia IPR1bits.RCIP = 1; //Sallitaan kaikki korkean //prioriteetin keskeytykset INTCONbits.GIEH = 1; //Loopataan ikuisesti ja //odotetaan keskeytyksia while (1); }
void main(void) { char c; signed char length; unsigned char msgtype; unsigned char last_reg_recvd; uart_comm uc; i2c_comm ic; unsigned char msgbuffer[MSGLEN + 1]; unsigned char i; uart_thread_struct uthread_data; // info for uart_lthread timer1_thread_struct t1thread_data; // info for timer1_lthread timer0_thread_struct t0thread_data; // info for timer0_lthread #ifdef __USE18F2680 OSCCON = 0xFC; // see datasheet // We have enough room below the Max Freq to enable the PLL for this chip OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line #else #ifdef __USE18F45J10 OSCCON = 0x82; // see datasheeet OSCTUNEbits.PLLEN = 1; // Makes the clock exceed the PIC's rated speed if the PLL is on 48 HZ #else #ifdef __USE18F26J50 OSCCON = 0xE0; // see datasheeet OSCTUNEbits.PLLEN = 1; #else #ifdef __USE18F46J50 OSCCON = 0xE0; //see datasheet OSCTUNEbits.PLLEN = 1; #else Something is messed up. The PIC selected is not supported or the preprocessor directives are wrong. #endif #endif #endif #endif // initialize my uart recv handling code init_uart_recv(&uc); // initialize the i2c code init_i2c(&ic); // init the timer1 lthread init_timer1_lthread(&t1thread_data); // initialize message queues before enabling any interrupts init_queues(); #ifndef __USE18F26J50 // set direction for PORTB to output PORTB = 0xFF; //TRISB = 0x0; //LATB = 0x0; #endif // how to set up PORTA for input (for the V4 board with the PIC2680) /* PORTA = 0x0; // clear the port LATA = 0x0; // clear the output latch ADCON1 = 0x0F; // turn off the A2D function on these pins // Only for 40-pin version of this chip CMCON = 0x07; // turn the comparator off TRISA = 0x0F; // set RA3-RA0 to inputs */ // initialize Timers OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_EDGE_RISE & T0_SOURCE_EXT & T0_PS_1_1); OpenTimer1(TIMER_INT_ON& T1_SYNC_EXT_OFF & T1_8BIT_RW & T1_SOURCE_EXT & T1_OSC1EN_OFF & T1_PS_1_1); //OpenTimer2(TIMER_INT_ON & T2_PS_1_16) #ifdef __USE18F26J50 // MTJ added second argument for OpenTimer1() OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0); #else #ifdef __USE18F46J50 // OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0); #else //OpenTimer1(TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); #endif #endif // Decide on the priority of the enabled peripheral interrupts // 0 is low, 1 is high // Timer1 interrupt IPR1bits.TMR1IP = 0; // USART RX interrupt IPR1bits.RCIP = 0; // I2C interrupt IPR1bits.SSPIP = 1; // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D) #if 1 // Note that the temperature sensor Address bits (A0, A1, A2) are also the // least significant bits of LATB -- take care when changing them // They *are* changed in the timer interrupt handlers if those timers are // enabled. They are just there to make the lights blink and can be // disabled. i2c_configure_slave(0x9E); #else // If I want to test the temperature sensor from the ARM, I just make // sure this PIC does not have the same address and configure the // temperature sensor address bits and then just stay in an infinite loop i2c_configure_slave(0x9A); #ifdef __USE18F2680 LATBbits.LATB1 = 1; LATBbits.LATB0 = 1; LATBbits.LATB2 = 1; #endif for (;;); #endif // must specifically enable the I2C interrupts PIE1bits.SSPIE = 1; // configure the hardware USART device #ifdef __USE18F26J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else #ifdef __USE18F46J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x26); //Low & 0x26 for 48Mhz //High & 0x26 #endif #endif // Peripheral interrupts can have their priority set to high or low // enable high-priority interrupts and low-priority interrupts enable_interrupts(); /* Junk to force an I2C interrupt in the simulator (if you wanted to) PIR1bits.SSPIF = 1; _asm goto 0x08 _endasm; */ // printf() is available, but is not advisable. It goes to the UART pin // on the PIC and then you must hook something up to that to view it. // It is also slow and is blocking, so it will perturb your code's operation // Here is how it looks: printf("Hello\r\n"); // loop forever // This loop is responsible for "handing off" messages to the subroutines // that should get them. Although the subroutines are not threads, but // they can be equated with the tasks in your task diagram if you // structure them properly. TRISCbits.RC0 = 1; while (1) { // Call a routine that blocks until either on the incoming // messages queues has a message (this may put the processor into // an idle mode) block_on_To_msgqueues(); // At this point, one or both of the queues has a message. It // makes sense to check the high-priority messages first -- in fact, // you may only want to check the low-priority messages when there // is not a high priority message. That is a design decision and // I haven't done it here. length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { case MSGT_TIMER0: { timer0_lthread(&t0thread_data, msgtype, length, msgbuffer); break; }; case MSGT_I2C_DATA: { break; }; case MSGT_I2C_DBG: { // Here is where you could handle debugging, if you wanted // keep track of the first byte received for later use (if desired) last_reg_recvd = msgbuffer[0]; break; }; case MSGT_I2C_RQST: { // Generally, this is *NOT* how I recommend you handle an I2C slave request // I recommend that you handle it completely inside the i2c interrupt handler // by reading the data from a queue (i.e., you would not send a message, as is done // now, from the i2c interrupt handler to main to ask for data). // // The last byte received is the "register" that is trying to be read // The response is dependent on the register. start_i2c_slave_reply(length, msgbuffer); break; }; case MSGT_MOTOR_COMMAND: { motor_lthread(msgtype,length,msgbuffer); break; }; case MSGT_MOTOR_STOP: { motor_lthread(msgtype,length,msgbuffer); break; }; case MSGT_UART_SEND: { uart_lthread(&uthread_data,msgtype,length,msgbuffer); break; }; default: { // Your code should handle this error break; }; }; } // Check the low priority queue length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // Your code should handle this situation } } else { switch (msgtype) { case MSGT_PARSE: { //parser_lthread(msgtype,length,msgbuffer); break; }; case MSGT_OVERRUN: case MSGT_UART_DATA: { uart_lthread(&uthread_data, msgtype, length, msgbuffer); break; }; default: { // Your code should handle this error break; }; }; } } }
void main(void) { char c; signed char length; unsigned char msgtype; unsigned char last_reg_recvd; uart_comm uc; i2c_comm ic; unsigned char msgbuffer[MSGLEN + 1]; unsigned char i; uart_thread_struct uthread_data; // info for uart_lthread timer1_thread_struct t1thread_data; // info for timer1_lthread timer0_thread_struct t0thread_data; // info for timer0_lthread #ifdef __USE18F2680 OSCCON = 0xFC; // see datasheet // We have enough room below the Max Freq to enable the PLL for this chip OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line #else #ifdef __USE18F45J10 OSCCON = 0x82; // see datasheeet OSCTUNEbits.PLLEN = 0; // Makes the clock exceed the PIC's rated speed if the PLL is on #else #ifdef __USE18F26J50 OSCCON = 0xE0; // see datasheeet OSCTUNEbits.PLLEN = 1; #else #ifdef __USE18F46J50 OSCCON = 0xE0; //see datasheet OSCTUNEbits.PLLEN = 1; #else Something is messed up. The PIC selected is not supported or the preprocessor directives are wrong. #endif #endif #endif #endif // initialize my uart recv handling code init_uart_recv(&uc); // initialize the i2c code init_i2c(&ic); // init the timer1 lthread init_timer1_lthread(&t1thread_data); // initialize message queues before enabling any interrupts init_queues(); #ifndef __USE18F26J50 // set direction for PORTB to output TRISB = 0xFF; //input LATB = 0x0; PORTA = 0x0; LATA = 0x0; TRISA = 0x0F; #endif // how to set up PORTA for input (for the V4 board with the PIC2680) /* PORTA = 0x0; // clear the port LATA = 0x0; // clear the output latch ADCON1 = 0x0F; // turn off the A2D function on these pins // Only for 40-pin version of this chip CMCON = 0x07; // turn the comparator off TRISA = 0x0F; // set RA3-RA0 to inputs */ // initialize Timers #ifdef MOTORPIC OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_EXT & T0_EDGE_RISE & T0_PS_1_1); #else OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_64); #endif #ifdef __USE18F26J50 // MTJ added second argument for OpenTimer1() OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0); #else #ifdef __USE18F46J50 OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0); #else OpenTimer1(TIMER_INT_ON & T1_8BIT_RW & T1_PS_1_1 & T1_SOURCE_EXT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); TRISC = 0xFF; // C as input #endif #endif // Decide on the priority of the enabled peripheral interrupts // 0 is low, 1 is high // ADC interrupt IPR1bits.ADIP = 0; PIE1bits.ADIE = 1; // Timer1 interrupt IPR1bits.TMR1IP = 0; // Timer0 interrupt INTCON2bits.TMR0IP = 1; // USART RX interrupt IPR1bits.RCIP = 0; // USART TX interrupt IPR1bits.TXIP = 0; // I2C interrupt IPR1bits.SSPIP = 1; // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D) #if 1 // Note that the temperature sensor Address bits (A0, A1, A2) are also the // least significant bits of LATB -- take care when changing them // They *are* changed in the timer interrupt handlers if those timers are // enabled. They are just there to make the lights blink and can be // disabled. #ifdef I2CMASTER i2c_configure_master(); #else #ifdef SENSORPIC i2c_configure_slave(0x9E); // slave addr 4F #else #ifdef MOTORPIC i2c_configure_slave(0xBE); // slave addr 5F #endif #endif #endif #else // If I want to test the temperature sensor from the ARM, I just make // sure this PIC does not have the same address and configure the // temperature sensor address bits and then just stay in an infinite loop i2c_configure_slave(0x9A); #ifdef __USE18F2680 LATBbits.LATB1 = 1; LATBbits.LATB0 = 1; LATBbits.LATB2 = 1; #endif for (;;); #endif // must specifically enable the I2C interrupts PIE1bits.SSPIE = 1; // configure the hardware USART device #ifdef __USE18F26J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else #ifdef __USE18F46J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else OpenUSART(USART_TX_INT_ON & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 38); RCSTAbits.SPEN = 1; TRISC = 0xFF; #endif #endif // Peripheral interrupts can have their priority set to high or low // enable high-priority interrupts and low-priority interrupts enable_interrupts(); /* Junk to force an I2C interrupt in the simulator (if you wanted to) PIR1bits.SSPIF = 1; _asm goto 0x08 _endasm; */ // printf() is available, but is not advisable. It goes to the UART pin // on the PIC and then you must hook something up to that to view it. // It is also slow and is blocking, so it will perturb your code's operation // Here is how it looks: printf("Hello\r\n"); OpenADC(ADC_FOSC_16 & ADC_LEFT_JUST & ADC_2_TAD, ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0b1011); SetChanADC(ADC_CH1); // ADC_CALIB(); // loop forever // This loop is responsible for "handing off" messages to the subroutines // that should get them. Although the subroutines are not threads, but // they can be equated with the tasks in your task diagram if you // structure them properly. unsigned char msg[2] = {0x01, 0x02}; //i2c_master_send(1, 5, msg, 0x9E); // send length, recv length, message and address + r/w bit (0) //i2c_master_recv(); //uart_trans(2, msg); //WriteUSART(0xAA); while (1) { // Call a routine that blocks until either on the incoming // messages queues has a message (this may put the processor into // an idle mode) block_on_To_msgqueues(); // At this point, one or both of the queues has a message. It // makes sense to check the high-priority messages first -- in fact, // you may only want to check the low-priority messages when there // is not a high priority message. That is a design decision and // I haven't done it here. length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { case MSGT_TIMER0: { timer0_lthread(&t0thread_data, msgtype, length, msgbuffer); break; }; case MSGT_I2C_DATA: case MSGT_I2C_DBG: { // Here is where you could handle debugging, if you wanted // keep track of the first byte received for later use (if desired) last_reg_recvd = msgbuffer[0]; break; }; case MSGT_I2C_MASTER_RECV_COMPLETE: { //msgbuffer[0] = length; uart_trans(length, msgbuffer); //i2c_master_send(1, 5, msg, 0x9E); break; }; case MSGT_I2C_MASTER_RECV_FAILED: { //unsigned char msg2[2] = {0xEE, 0xFF}; //i2c_master_send(1, 5, msg, 0x9E); //LATBbits.LATB2 = 0; break; }; default: { // Your code should handle this error break; }; }; } // Check the low priority queue length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // Your code should handle this situation } } else { switch (msgtype) { case MSGT_TIMER1: { timer1_lthread(&t1thread_data, msgtype, length, msgbuffer); break; }; case MSGT_OVERRUN: case MSGT_UART_DATA: { //uart_trans(2, msgbuffer); if(msgbuffer[0] == 0xBA){ // motor command i2c_master_send(5, 5, msgbuffer, 0xBE); } else if(msgbuffer[0] == 0xAA){ // sensor command i2c_master_send(1, 5, msgbuffer, 0x9E); } LATBbits.LATB2 = 0; //uart_lthread(&uthread_data, msgtype, length, msgbuffer); break; }; default: { // Your code should handle this error break; }; }; } } }
void main(void) { char c; int count = 0; signed char length; unsigned char msgtype; unsigned char last_reg_recvd; uart_comm uc; i2c_comm ic; unsigned char msgbuffer[MSGLEN + 1]; unsigned char i; uart_thread_struct uthread_data; // info for uart_lthread timer1_thread_struct t1thread_data; // info for timer1_lthread timer0_thread_struct t0thread_data; // info for timer0_lthread unsigned char config1 = 0x00, config2 = 0x00, portconfig = 0x00; #ifdef __USE18F2680 OSCCON = 0xFC; // see datasheet // We have enough room below the Max Freq to enable the PLL for this chip OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line #else #ifdef __USE18F45J10 OSCCON = 0x82; // see datasheeet OSCTUNEbits.PLLEN = 0; // Makes the clock exceed the PIC's rated speed if the PLL is on #else #ifdef __USE18F26J50 OSCCON = 0xE0; // see datasheeet OSCTUNEbits.PLLEN = 1; #else #ifdef __USE18F46J50 OSCCON = 0xE0; //see datasheet OSCTUNEbits.PLLEN = 1; #else Something is messed up. The PIC selected is not supported or the preprocessor directives are wrong. #endif #endif #endif #endif // initialize my uart recv handling code init_uart_recv(&uc); // initialize the code init_i2c(&ic); // init the timer1 lthread init_timer1_lthread(&t1thread_data); // initialize message queues before enabling any interrupts init_queues(); #ifndef __USE18F26J50 // // set direction for PORTB to output // TRISB = 0x0; // LATB = 0x0; #endif PORTB = 0xFF; // Setup PORTBs for debug pins. TRISBbits.RB1 = 0; TRISBbits.RB2 = 0; TRISBbits.RB3 = 0; TRISBbits.RB4 = 0; TRISBbits.RB5 = 0; LATBbits.LATB1 = 0; LATBbits.LATB2 = 0; LATBbits.LATB3 = 0; LATBbits.LATB4 = 0; LATBbits.LATB5 = 0; //line sensor PORTD = 0x0; LATD = 0x0; TRISD3 = 0x1; TRISD4 = 0x1; TRISD5 = 0x1; TRISD6 = 0x1; TRISD7 = 0x1; // initialize Timers OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_EXT & T0_PS_1_1); #ifdef __USE18F26J50 // MTJ added second argument for OpenTimer1() OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF, 0x0); #else #ifdef __USE18F46J50 OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF, 0x0); #else OpenTimer1(TIMER_INT_ON & T1_PS_1_1 & T1_16BIT_RW & T1_SOURCE_EXT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); WriteTimer1(0xFFCF); #endif #endif // Decide on the priority of the enabled peripheral interrupts // 0 is low, 1 is high // Timer1 interrupt IPR1bits.TMR1IP = 0; // USART TX interrupt IPR1bits.TXIP = 0; // I2C interrupt IPR1bits.SSPIP = 1; // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D) #if 1 // Note that the temperature sensor Address bits (A0, A1, A2) are also the // least significant bits of LATB -- take care when changing them // They *are* changed in the timer interrupt handlers if those timers are // enabled. They are just there to make the lights blink and can be // disabled. i2c_configure_slave(0x9E); #else // If I want to test the temperature sensor from the ARM, I just make // sure this PIC does not have the same address and configure the // temperature sensor address bits and then just stay in an infinite loop i2c_configure_slave(0x9A); #ifdef __USE18F2680 LATBbits.LATB1 = 1; LATBbits.LATB0 = 1; LATBbits.LATB2 = 1; #endif for (;;); #endif // must specifically enable the I2C interrupts PIE1bits.SSPIE = 1; PIE1bits.RCIE = 1; // configure the hardware USART device #ifdef __USE18F26J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else #ifdef __USE18F46J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else // Configuration Details: // Solve for SPBRG = ((48Mhz/19200)/16)-1 = 155 // Calculated Baud Rate = 48MHz / (4*(624 + 1)) = 19200 // Error (19200 - 19200) / 19200 = 0 OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 39); BAUDCONbits.BRG16 = 0; RCSTAbits.SPEN = 1; RCSTAbits.CREN = 1; #endif #endif // Peripheral interrupts can have their priority set to high or low // enable high-priority interrupts and low-priority interrupts enable_interrupts(); // loop forever // This loop is responsible for "handing off" messages to the subroutines // that should get them. Although the subroutines are not threads, but // they can be equated with the tasks in your task diagram if you // structure them properly. while (1) { // Call a routine that blocks until either on the incoming // messages queues has a message (this may put the processor into // an idle mode) block_on_To_msgqueues(); // At this point, one or both of the queues has a message. It // makes sense to check the high-priority messages first -- in fact, // you may only want to check the low-priority messages when there // is not a high priority message. That is a design decision and // I haven't done it here. length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { case MSGT_I2C_DATA: { uart_lthread(&uthread_data, msgtype, length, msgbuffer); break; }; default: { // Your code should handle this error break; }; }; } // Check the low priority queue length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); } }
void main (void) { /* Define Variables --------------------------------------------------------------------- */ // I2C/MSG Q variables char c; // Is this used? signed char length; unsigned char msgtype; unsigned char last_reg_recvd; i2c_comm ic; //unsigned char msgbuffer[MSGLEN+1]; unsigned char msgbuffer[12]; unsigned char i; int I2C_buffer[]; int index = 0; int ITR = 0; int I2C_RX_MSG_COUNT = 0; int I2C_RX_MSG_PRECOUNT = 0; int I2C_TX_MSG_COUNT = 1; // Timer variables timer1_thread_struct t1thread_data; // info for timer1_lthread timer0_thread_struct t0thread_data; // info for timer0_lthread int timer_on = 1; int timer2Count0 = 0, timer2Count1 = 0; // UART variables uart_comm uc; //uart_thread_struct uthread_data; // info for uart_lthread // ADC variables int ADCVALUE = 0; int adc_counter = 0; int adc_chan_num = 0; int adcValue = 0; int count = 0; // MIDI variable char notePlayed; /* Initialization ------------------------------------------------------------------------ */ // Clock initialization OSCCON = 0x7C; // 16 MHz // Use for internal oscillator OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line // UART initialization init_uart_recv(&uc); // initialize my uart recv handling code // configure the hardware USART device Open2USART( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 31); Open1USART( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51); //RCSTA1bits.CREN = 1; //RCSTA1bits.SPEN = 1; //TXSTA1bits.SYNC = 0; //PIE1bits.RC1IE = 1; IPR1bits.RC1IP = 0; // I2C/MSG Q initialization init_i2c(&ic); // initialize the i2c code init_queues(); // initialize message queues before enabling any interrupts i2c_configure_slave(0x9E); // configure the hardware i2c device as a slave // Timer initialization init_timer1_lthread(&t1thread_data); // init the timer1 lthread OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_8); OpenTimer2( TIMER_INT_ON & T2_PS_1_16 /*& T2_8BIT_RW & T2_SOURCE_INT & T2_OSC1EN_OFF & T2_SYNC_EXT_OFF*/); // Turn Off // ADC initialization // set up PORTA for input PORTA = 0x0; // clear the port LATA = 0x0; // clear the output latch TRISA = 0xFF; // set RA3-RA0 to inputs ANSELA = 0xFF; initADC(); // Interrupt initialization // Peripheral interrupts can have their priority set to high or low // enable high-priority interrupts and low-priority interrupts enable_interrupts(); // Decide on the priority of the enabled peripheral interrupts, 0 is low 1 is high IPR1bits.TMR1IP = 0; // Timer1 interrupt //IPR1bits.RCIP = 0; // USART RX interrupt IPR1bits.SSP1IP = 1; // I2C interrupt PIE1bits.SSP1IE = 1; // must specifically enable the I2C interrupts IPR1bits.ADIP = 1; // ADC interrupt WE ADDED THIS // set direction for PORTB to output TRISB = 0x0; TRISD = 0xFF; LATB = 0x0; ANSELC = 0x00; /* Hand off messages to subroutines ----------------------------------------------------------- */ // This loop is responsible for "handing off" messages to the subroutines // that should get them. Although the subroutines are not threads, but // they can be equated with the tasks in your task diagram if you // structure them properly. while (1) { // Call a routine that blocks until either on the incoming // messages queues has a message (this may put the processor into // an idle mode block_on_To_msgqueues(); /* High Priority MSGQ ---------------------------------------------------------------------- */ // At this point, one or both of the queues has a message. It // makes sense to check the high-priority messages first -- in fact, // you may only want to check the low-priority messages when there // is not a high priority message. That is a design decision and // I haven't done it here. length = ToMainHigh_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { //printf("Error: Bad high priority receive, code = %x\r\n", length); } } else { switch (msgtype) { case MSGT_ADC: { // Format I2C msg msgbuffer[6] = (timer2Count0 & 0x00FF); msgbuffer[5] = (timer2Count0 & 0xFF00) >> 8; msgbuffer[4] = (timer2Count1 & 0x00FF); msgbuffer[3] = (timer2Count1 & 0xFF00) >> 8; msgbuffer[8] = 0x00; msgbuffer[10] = adc_chan_num; msgbuffer[11] = 0xaa; // ADC MSG opcode // Send I2C msg FromMainHigh_sendmsg(12, msgtype, msgbuffer); // Send ADC msg to FromMainHigh MQ, which I2C // int hdlr later Reads // Increment I2C message count from 1 to 100 if(I2C_TX_MSG_COUNT < 100) { I2C_TX_MSG_COUNT = I2C_TX_MSG_COUNT + 1; } else { I2C_TX_MSG_COUNT = 1; } // Increment the channel number if(adc_chan_num <= 4) adc_chan_num++; else adc_chan_num = 0; // Set ADC channel based off of channel number if(adc_chan_num == 0) SetChanADC(ADC_CH0); else if(adc_chan_num == 1) SetChanADC(ADC_CH1); else if(adc_chan_num == 2) SetChanADC(ADC_CH2); else if(adc_chan_num == 3) SetChanADC(ADC_CH3); else if(adc_chan_num == 4) SetChanADC(ADC_CH4); else SetChanADC(ADC_CH5); }; case MSGT_TIMER0: { timer0_lthread(&t0thread_data,msgtype,length,msgbuffer); break; }; case MSGT_TIMER2: { timer2Count0++; if(timer2Count0 >= 0xFFFF) { timer2Count1++; timer2Count0 = 0; } break; } case MSGT_I2C_DATA: { //this data still needs to be put in a buffer ; if(msgbuffer[0] == 0xaf) { //FromMainLow_sendmsg(5, msgtype, msgbuffer); // The code below checks message 'counts' to see if any I2C messages were dropped //I2C_RX_MSG_COUNT = msgbuffer[4]; FromMainLow_sendmsg(9, msgtype, msgbuffer); TXSTA2bits.TXEN = 1; /* // Send note data to the MIDI device //while(Busy2USART()); putc2USART(msgbuffer[1]); //while(Busy2USART()); Delay1KTCYx(8); putc2USART(msgbuffer[2]); //while(Busy2USART()); Delay1KTCYx(8); putc2USART(msgbuffer[3]); */ if(I2C_RX_MSG_COUNT - I2C_RX_MSG_PRECOUNT == 1) { if(I2C_RX_MSG_PRECOUNT < 99) { I2C_RX_MSG_PRECOUNT++; } else { I2C_RX_MSG_PRECOUNT = 0; } } else { I2C_RX_MSG_PRECOUNT = I2C_RX_MSG_COUNT; } } }; ` case MSGT_I2C_DBG: { //printf("I2C Interrupt received %x: ",msgtype); for (i=0;i<length;i++) { //printf(" %x",msgbuffer[i]); } //printf("\r\n"); // keep track of the first byte received for later use last_reg_recvd = msgbuffer[0]; break; }; case MSGT_I2C_RQST: { //printf("I2C Slave Req\r\n"); // The last byte received is the "register" that is trying to be read // The response is dependent on the register. switch (last_reg_recvd) { case 0xaa: { break; } /* case 0xa8: { length = 1; msgbuffer[0] = 0x3A; break; } case 0xa9: { length = 1; msgbuffer[0] = 0xA3; break; }*/ }; //start_i2c_slave_reply(length,msgbuffer); break; }; default: { //printf("Error: Unexpected msg in queue, type = %x\r\n", msgtype); break; }; }; } /* Low Priority MSGQ ----------------------------------------------------------------------- */ length = ToMainLow_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { } } else { switch (msgtype) { case MSGT_TIMER1: { timer1_lthread(&t1thread_data,msgtype,length,msgbuffer); break; }; case MSGT_OVERRUN: case MSGT_UART_DATA: { LATB = 0xFF; msgbuffer[11] = 0xBB; FromMainHigh_sendmsg(12, msgtype, msgbuffer); break; }; default: { break; }; }; } }
void main(void) { //char c; signed char length; unsigned char msgtype; unsigned char last_reg_recvd; uart_comm uc; i2c_comm ic; unsigned char msgbuffer[MSGLEN + 1]; // unsigned char to_send_buffer[MAX_I2C_SENSOR_DATA_LEN + HEADER_MEMBERS]; // uint8 to_send_len; // int data_points_count = 0; //unsigned char i; //uart_thread_struct uthread_data; // info for uart_lthread //timer1_thread_struct t1thread_data; // info for timer1_lthread //timer0_thread_struct t0thread_data; // info for timer0_lthread #ifdef __USE18F2680 OSCCON = 0xFC; // see datasheet // We have enough room below the Max Freq to enable the PLL for this chip OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line #else #ifdef __USE18F45J10 OSCCON = 0x82; // see datasheeet OSCTUNEbits.PLLEN = 0; // Makes the clock exceed the PIC's rated speed if the PLL is on #else #ifdef __USE18F26J50 OSCCON = 0xE0; // see datasheeet OSCTUNEbits.PLLEN = 1; #else #ifdef __USE18F46J50 OSCCON = 0xE0; //see datasheet OSCTUNEbits.PLLEN = 1; #else Something is messed up. The PIC selected is not supported or the preprocessor directives are wrong. #endif #endif #endif #endif // initialize my uart recv handling code init_uart_recv(&uc); // initialize the i2c code init_i2c(&ic); // init the timer1 lthread // init_timer1_lthread(&t1thread_data); // initialize message queues before enabling any interrupts init_queues(); #ifndef __USE18F26J50 // set direction for PORTB to output #ifndef MOTOR_PIC TRISB = 0x0; LATB = 0x0; #else TRISBbits.RB0 = 0; TRISBbits.RB1 = 0; TRISBbits.RB2 = 0; TRISBbits.RB3 = 0; #endif #endif // how to set up PORTA for input (for the V4 board with the PIC2680) /* PORTA = 0x0; // clear the port LATA = 0x0; // clear the output latch ADCON1 = 0x0F; // turn off the A2D function on these pins // Only for 40-pin version of this chip CMCON = 0x07; // turn the comparator off TRISA = 0x0F; // set RA3-RA0 to inputs */ // initialize Timers #ifndef MASTER_PIC #ifdef SENSOR_PIC OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16); INTCONbits.T0IE = 1; #elif !defined(MOTOR_PIC) OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4); #else OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_PS_1_1 & T0_SOURCE_EXT); #endif #else OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16); #endif #ifdef __USE18F26J50 // MTJ added second argument for OpenTimer1() OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF, 0x0); #else #ifdef __USE18F46J50 //OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF, 0x0); #else #ifndef MOTOR_PIC // OpenTimer1(TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); #else OpenTimer1(TIMER_INT_ON & T1_PS_1_1 & T1_16BIT_RW & T1_SOURCE_EXT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF); WRITETIMER1(0xFFE8); // leave in here, encoder's for motor 2 doesn't work without it #endif #endif #endif // Decide on the priority of the enabled peripheral interrupts // 0 is low, 1 is high //Timer0 interrupt INTCON2bits.TMR0IP = 1; // Timer1 interrupt #ifdef SENSOR_PIC // IPR1bits.TMR1IP = 0; #else IPR1bits.TMR1IP = 1; #endif // USART RX interrupt IPR1bits.RCIP = 0; IPR1bits.TXIP = 0; // I2C interrupt IPR1bits.SSPIP = 1; //set i2c int high PIE1bits.SSPIE = 1; #ifdef SENSOR_PIC //resetAccumulators(); init_adc(); initUS(); // must specifically enable the I2C interrupts IPR1bits.ADIP = 0; // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D) i2c_configure_slave(SENSOR_ADDR << 1); //address 0x10 #elif defined MOTOR_PIC i2c_configure_slave(MOTOR_ADDR << 1); //address 0x20 #elif defined PICMAN i2c_configure_slave(PICMAN_ADDR << 1); //address 0x10,different bus from sensor #elif defined I2C_MASTER //sending clock frequency i2c_configure_master(); //12MHz clock set hardcoded #endif #ifdef MASTER_PIC ///////Color Sensor Interrupt////////// TRISBbits.TRISB0 = 1; ANCON1bits.PCFG12 = 1; //not sure which is port b INTCONbits.INT0IE = 1; INTCON2bits.INTEDG0 = 0; INTCONbits.INT0IF = 0; initializeColorSensor(); /////////////////////////////////////// #endif // configure the hardware USART device #ifdef __USE18F26J50 Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #else #ifdef __USE18F46J50 #ifndef MOTOR_PIC Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 38); #else Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 0x19); #endif #else OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 38); // BAUDCONbits.BRG16 = 1; // TXSTAbits.TXEN = 1; // RCSTAbits.SPEN = 1; // RCSTAbits.CREN = 1; #endif #endif // Peripheral interrupts can have their priority set to high or low // enable high-priority interrupts and low-priority interrupts enable_interrupts(); LATBbits.LB7 = 0; #ifndef MASTER_PIC LATBbits.LB0 = 0; #endif LATBbits.LB1 = 0; LATBbits.LB2 = 0; LATBbits.LB3 = 0; WRITETIMER0(0x00FF); // loop forever // This loop is responsible for "handing off" messages to the subroutines // that should get them. Although the subroutines are not threads, but // they can be equated with the tasks in your task diagram if you // structure them properly. while (1) { // Call a routine that blocks until either on the incoming // messages queues has a message (this may put the processor into // an idle mode) block_on_To_msgqueues(); //We have a bunch of queues now - ToMainHigh, ToMainLow, FromMainHigh, FromMainLow, //FromUARTInt, and FromI2CInt //From queues are most important because they will be called repeatedly with busy info //Int queues are second because we'll often get data from either UART or I2C //ToMain are least length = FromMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { #ifdef I2C_MASTER case MSGT_MASTER_RECV_BUSY: { //retry //debugNum(4); i2c_master_recv(msgbuffer[0]); break; }; case MSGT_MASTER_SEND_BUSY: { //retry //debugNum(8); i2c_master_send(msgbuffer[0], length - 1, msgbuffer + 1); // point to second position (actual msg start) break; }; case MSGT_MASTER_SEND_NO_RAW_BUSY: { i2c_master_send_no_raw(msgbuffer[0], length-1, msgbuffer + 1); }; case MSGT_TURN_CHECK: { //check IR sensors unsigned char frame[FRAME_MEMBERS] = {0}; packFrame(frame, sizeof frame); //frame[1] is ir1 and frame[2] is ir2 frame[1] = 1;//just for now, provide these dummy values frame[2] = 1; if((frame[1] > frame[2]) && (frame[1] - frame[2]) > 10){ //readjust right char out[HEADER_MEMBERS] = {0}; uint8 len = generateReadjustCW(out, sizeof out, I2C_COMM); i2c_master_send(MOTOR_ADDR, len, out); // no need to call waitForSensorFrame() again } else if((frame[2] > frame[1]) && (frame[2] - frame[1]) > 10){ //readjust left char out[HEADER_MEMBERS] = {0}; uint8 len = generateReadjustCCW(out, sizeof out, I2C_COMM); i2c_master_send(MOTOR_ADDR, len, out); // no need to call waitForSensorFrame() again } else{ char command[HEADER_MEMBERS] = {0}; uint8 len = generateTurnCompleteReq(command, sizeof command, UART_COMM); //tell picman turn complete uart_send_array(command, len); turnCompleted(); } }; #endif case MSGT_UART_TX_BUSY: { // TODO: take out for now uart_send_array(msgbuffer, length); break; }; default: break; } } length = FromUARTInt_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { case MSGT_OVERRUN: break; case MSGT_UART_DATA: { #ifdef PICMAN setRoverDataLP(msgbuffer); handleRoverDataLP(); #elif defined(MASTER_PIC) || defined(ROVER_EMU) setBrainDataLP(msgbuffer); //pass data received and tell will pass over i2c handleMessageLP(UART_COMM, I2C_COMM); //sends the response and then sets up the command handling #endif break; }; case MSGT_UART_RECV_FAILED: { debugNum(2); debugNum(4); debugNum(2); debugNum(4); break; }; case MSGT_UART_TX_BUSY: { // TODO: take out for now uart_send_array(msgbuffer, length); break; }; default: break; } } length = FromI2CInt_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { case MSGT_I2C_DATA: { #if defined(MASTER_PIC) || defined(ARM_EMU) //handle whatever data will come through via i2c //msgbuffer can hold real data - error codes will be returned through the error cases setRoverDataLP(msgbuffer); handleRoverDataLP(); #else setBrainDataLP(msgbuffer); #endif break; }; case MSGT_I2C_RQST: { #if defined(MOTOR_PIC) || defined(SENSOR_PIC) handleMessageLP(I2C_COMM, I2C_COMM); #elif defined(PICMAN) handleMessageLP(I2C_COMM, UART_COMM); #endif break; }; case MSGT_I2C_DBG: { // Here is where you could handle debugging, if you wanted // keep track of the first byte received for later use (if desired) last_reg_recvd = msgbuffer[0]; break; }; #ifdef MASTER_PIC case MSGT_I2C_MASTER_RECV_FAILED: { uart_send_array(msgbuffer, length); break; }; case MSGT_I2C_MASTER_SEND_FAILED: { uart_send_array(msgbuffer, length); break; }; case MSGT_MASTER_RECV_BUSY: { //retry // debugNum(4); i2c_master_recv(msgbuffer[0]); break; }; case MSGT_MASTER_SEND_BUSY: { //retry // debugNum(8); i2c_master_send(msgbuffer[0], length - 1, msgbuffer + 1); // point to second position (actual msg start) break; }; case MSGT_COLOR_SENSOR_INIT: { initializeColorSensorStage(); }; #endif default: break; } } length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // This case be handled by your code. } } else { switch (msgtype) { case MSGT_TIMER0: { //timer0_lthread(&t0thread_data, msgtype, length, msgbuffer); break; }; // #ifdef I2C_MASTER // case MSGT_MASTER_RECV_BUSY: // { // //retry // debugNum(4); // i2c_master_recv(msgbuffer[0]); // break; // }; // case MSGT_MASTER_SEND_BUSY: // { // //retry // debugNum(8); // i2c_master_send(msgbuffer[0], length-1, msgbuffer + 1); // point to second position (actual msg start) // break; // }; // #endif // case MSGT_I2C_DATA: // { // debugNum(4); //#if defined(MASTER_PIC) || defined(ARM_EMU) // //handle whatever data will come through via i2c // //msgbuffer can hold real data - error codes will be returned through the error cases // setRoverDataLP(msgbuffer); // handleRoverDataLP(); //#else // setBrainDataLP(msgbuffer); //#endif // debugNum(4); // break; // }; // case MSGT_I2C_RQST: // { //#if defined(MOTOR_PIC) || defined(SENSOR_PIC) // handleMessageLP(I2C_COMM, I2C_COMM); //#elif defined(PICMAN) // handleMessageLP(I2C_COMM, UART_COMM); //#endif // break; // }; // case MSGT_I2C_DBG: // { // // Here is where you could handle debugging, if you wanted // // keep track of the first byte received for later use (if desired) // last_reg_recvd = msgbuffer[0]; // break; // }; //#ifdef MASTER_PIC // case MSGT_I2C_MASTER_RECV_FAILED: // { // uart_send_array(msgbuffer, length); // break; // }; // case MSGT_I2C_MASTER_SEND_FAILED: // { // uart_send_array(msgbuffer, length); // break; // }; //#endif case MSGT_AD: { #ifdef SENSOR_PIC //addDataPoints(sensorADid, msgbuffer, length); #endif break; }; default: { // Your code should handle this error break; }; }; } // Check the low priority queue length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer); if (length < 0) { // no message, check the error code to see if it is concern if (length != MSGQUEUE_EMPTY) { // Your code should handle this situation } } else { switch (msgtype) { case MSGT_AD: { #ifdef SENSOR_PIC //addDataPoints(sensorADid, msgbuffer, length); #endif break; }; case MSGT_TIMER1: { //timer1_lthread(&t1thread_data, msgtype, length, msgbuffer); break; }; // case MSGT_OVERRUN: // break; // case MSGT_UART_DATA: // { //#ifdef PICMAN // setRoverDataLP(msgbuffer); // handleRoverDataLP(); //#elif defined(MASTER_PIC) || defined(ROVER_EMU) // setBrainDataLP(msgbuffer);//pass data received and tell will pass over i2c // handleMessageLP(UART_COMM, I2C_COMM); //sends the response and then sets up the command handling //#endif // break; // }; // case MSGT_UART_RECV_FAILED: // { // debugNum(1); // debugNum(2); // debugNum(1); // debugNum(2); // break; // }; default: { // Your code should handle this error break; }; }; } } // }
void main(void) { #define BAUDRG 77 BYTE SecNum = 0; BOOL Tx_Success = FALSE; BYTE Tx_Trials = 0, scanresult = 0; /*******************************************************************/ // Initialize the system /*******************************************************************/ ANCON0 = 0XFF; /*desactiva entradas analogicas*/ ANCON1 = 0XFF; /*desactiva entradas analogicas*/ PPSUnLock(); PPSOutput(PPS_RP10, PPS_TX2CK2); // TX2 RP17/RC6 icsp PPSInput(PPS_RX2DT2, PPS_RP9); // RX2 RP18/RC7 PPSOutput(PPS_RP23, PPS_SDO2); // SDO2 RP23/RD6 PPSInput(PPS_SDI2, PPS_RP24); // SDI2 RP24/RD7 PPSOutput(PPS_RP22, PPS_SCK2); // SCK2 RP22/RD5 PPSLock(); System_PeripheralPinSelect( ExternalInterrupt3, 19); /*external interrupt 3 B3*/ BoardInit(); ConsoleInit(); Gpios_PinDirection(GPIOS_PORTC, 7, GPIOS_INPUT); /*pin C0 como salida para SDI*/ Gpios_PinDirection(GPIOS_PORTC, 6, GPIOS_OUTPUT); /*pin C1 como salida para SDO*/ //Gpios_PinDirection(GPIOS_PORTD, 4, GPIOS_OUTPUT); /*pin D4 como salida */ Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, BAUDRG); baud1USART(BAUD_IDLE_TX_PIN_STATE_HIGH & BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_OFF); Open2USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, BAUDRG); baud2USART(BAUD_IDLE_TX_PIN_STATE_HIGH & BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_OFF); OpenTimer1( TIMER_INT_OFF &T1_16BIT_RW &T1_SOURCE_FOSC_4 & T1_PS_1_8 &T1_OSC1EN_OFF &T1_SYNC_EXT_OFF, TIMER_GATE_OFF & TIMER_GATE_INT_OFF); Gpios_PinDirection(GPIOS_PORTD, 7, GPIOS_INPUT); /*pin C0 como salida para SDI*/ Gpios_PinDirection(GPIOS_PORTD, 6, GPIOS_OUTPUT); /*pin C1 como salida para SDO*/ Gpios_PinDirection(GPIOS_PORTD, 5, GPIOS_OUTPUT); /*pin C2 como salida para SCK*/ Spi_Init(SPI_PORT1, SPI_64DIV); /*Inicializamos SPI2*/ Spi_Init(SPI_PORT2, SPI_64DIV); /*Inicializamos SPI2*/ //Adc_Init(ADC_10BITS); LED_1 = 1; LED_2 = 0; //RLY_1 = 0; RLY_2 = 0; ON_RADIO = 1; ON_MAC = 1; ON_TEMP = 1; StartWirelessConnection(); //myDevicesRequiredStatus[0] = 0x55; EEPROMRead(&myDevicesRequiredStatus, 0, 1); if(myDevicesRequiredStatus[0] == 0x55) { RLY_1 = 1; EEPROMCHG = 1; ConsolePutROMString((ROM char *)"RELAY ON "); } if(myDevicesRequiredStatus[0] == 0xAA) { RLY_1 = 0; EEPROMCHG = 0; ConsolePutROMString((ROM char *)"RELAY OFF "); } for(j=0;j<10;j++) { DelayMs(50); LED_1 ^= 1; LED_2 ^= 1; } LED_1 = 0; LED_2 = 0; //RLY_1 = 0; RLY_2 = 0; TickScaler = 4; EndDevStateMachine =0; /* while(!Tx_Success) { if(myChannel < 8) scanresult = MiApp_SearchConnection(10, (0x00000001 << myChannel)); else if(myChannel < 16) scanresult = MiApp_SearchConnection(10, (0x00000100 << (myChannel-8))); else if(myChannel < 24) scanresult = MiApp_SearchConnection(10, (0x00010000 << (myChannel-16))); else scanresult = MiApp_SearchConnection(10, (0x01000000 << (myChannel-24))); if(scanresult == 0) { Tx_Trials++; if(Tx_Trials > 2) break; } else Tx_Success = TRUE; } if(Tx_Success) { ConsolePutROMString((ROM char *)"RADIO OK "); } else { ConsolePutROMString((ROM char *)"RADIO FAIL "); } */ //.VBGOE = 0; //ANCON1bits.VBGEN = 1; // Enable Band gap reference voltage //DelayMs(10); //VBGResult = Adc_u16Read(15); //ANCON1bits.VBGEN = 0; // Disable Bandgap //Adc_Init(ADC_10BITS); ANCON0 = 0xFF; ANCON1 = 0x9F; ANCON1bits.VBGEN = 1; // Enable Band gap reference voltage ADCON0bits.VCFG = 0; // vreff VDD-VSS ADCON0bits.CHS = 0x0F; // VBG channel select ADCON1 = 0xBE; ADCON0bits.ADON = 1; //for(j=0;j<16;j++) //{ //myDevicesOutputStatus[j] = j; //} //EEPROMWRITE(myDevicesOutputStatus,0,16); //for(j=0;j<16;j++) //{ //myDevicesOutputStatus[j] = 0; //} //DelayMs(500); EEPROMRead(&myDevicesOutputStatus, 0, 16); ConsolePutROMString((ROM char *)"EEPROM READ: "); //PrintChar(TemperatureCalibrationValue); for(j=0;j<1;j++) { PrintChar(myDevicesOutputStatus[j]); } SwTimer3 = 0; SwTimer4 = 0; TRISB&=0xEF; //JL: Configuro el pin B4 como salida si modificar el estado de los demas pines while(1) { /* WirelessTxRx(); WirelesStatus(); Bypass(); */ //No se utilizaron //Menu(); //Timer1Tick(); //WirelessTxRxPANCOORD(); //TaskScheduler(); //JLEstas funciones deben habilitarse para trabajar como repetidora Timer1Tick(); Repeater(); } }
void main(void) { // Enable UART for serial output display Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, BAUDRG); #ifdef INVERT_UART baud1USART(BAUD_IDLE_TX_PIN_STATE_LOW & BAUD_IDLE_RX_PIN_STATE_LOW & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_ON); #else baud1USART(BAUD_IDLE_TX_PIN_STATE_HIGH & BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_ON); #endif // I/O states will be held until DSCONL.RELEASE = 0, but we must still initialize // to what we want before clearing the RELEASE bit. InitializeIO(); #ifdef USE_32KHZ_CRYSTAL // Enable the Secondary Oscillator for RTCC use. OpenTimer1(TIMER_INT_OFF & T1_SOURCE_PINOSC & T1_PS_1_1 & T1_OSC1EN_ON & T1_SYNC_EXT_OFF, 0); #endif // Did we wake up from Deep Sleep? Or is this the first initial power on? if (WDTCONbits.DS) { // woke up from Deep Sleep DSCONLbits.RELEASE = 0; // release control and data bits for all I/Os wait(); // allow time for INTOSC to stablize before using UART printf("Woke from Deep Sleep via "); if (DSWAKEHbits.DSINT0 != 0) { printf("RB0/INT0\r\n"); } else if (DSWAKELbits.DSMCLR != 0) { printf("MCLR\r\n"); } else if (DSWAKELbits.DSFLT != 0) { printf("DSFLT\n"); } else if (DSWAKELbits.DSWDT != 0) { printf("DSWDT\n"); } else if (DSWAKELbits.DSRTC != 0) { printf("RTCC Alarm\n"); } } else { // first initial power up of the device. // unlock the RTCC registers so that we can write to them EECON2 = 0x55; EECON2 = 0xAA; RTCCFGbits.RTCWREN = 1; // reset RTCC date/time (only on first power on) RTCCFGbits.RTCPTR1 = 1; RTCCFGbits.RTCPTR0 = 1; RTCVALL = 0x09; // reserved | year RTCVALH = 0xFF; RTCVALL = 0x01; // month | day RTCVALH = 0x01; RTCVALL = 0x12; // weekday | hours RTCVALH = 0x01; RTCVALL = 0x00; // minutes | seconds RTCVALH = 0x00; RTCCFGbits.RTCEN = 1; // enable RTCC module RTCCAL = RTCCALIBRATION; wait(); // allow time for INTOSC to stablize before using UART } // Application Tasks DoApplicationTasks(); // Power down into Deep Sleep mode. printf("Powering down into deep sleep, push RB0/INT0 down to wake...\r\n"); Flush1USART(); Close1USART(); RB0ReleasedWait(); // wait for user to release INT0 so we don't get spurious wake up EnterDeepSleepMode: EnableINT0(); EnterDeepSleep(); // Execution should normally never reach here, as Deep Sleep powers up // at the Reset Vector. // // However, there exists a small possibility an INT0 wake up is triggered // before the processor has fully enter Deep Sleep mode. As a result, gaurd // code is required here to handle re-initiating Deep Sleep entry. goto EnterDeepSleepMode; }
void USARTconfig() { //UART Init Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 7); }