void rs232_clean(void) { if (rs232_initialized == 1) { CloseUSART(); rs232_initialized = 0; } }
int main(int argc, char** argv) { OSCCON = 0xF7; //0b11110111; //TRISD = 0x00; //LED output TRISC = 0x80; //RX: input, TX: output TRISB = 0x10; //Columns and rows = inputs, except column 1 = output LATB = 0x00; CloseUSART(); OpenUSART(RS232_CONFIG, RS232_SPBRG); baudUSART(RS232_BAUD); INTCON = 0xA0; //GIE = 1, TMR0IE = 1, TMR0IF = 0 while (1) { } return (EXIT_SUCCESS); }
void InitApp(void) { unsigned char config, baudconfig; unsigned spbrg; /* TODO Initialize User Ports/Peripherals/Project here */ /* Setup analog functionality and port direction */ SRAM_Setup_Pins(); /* Initialize peripherals */ CloseUSART(); config = USART_TX_INT_OFF & USART_RX_INT_ON & USART_BRGH_HIGH & USART_CONT_RX & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF; /* SPBRG based on 8MHz clk */ //spbrg = 16; //115200 //spbrg = 34; // 57600 spbrg = 103; // 19200 known working w/ 8MHz clock //spbrg = 207; // 9600 //spbrg = 832; // 2400 //spbrg = 1665; // 1200 /* SPBRG based on 48MHz clk */ //spbrg = 103; //115200 48MHz OpenUSART(config, spbrg); baudconfig = BAUD_IDLE_CLK_LOW & BAUD_16_BIT_RATE & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF; baudconfig &= ~0x20; // RX Not inverted baudUSART(baudconfig); PIR1bits.RCIF = 0; // Clear RX interrput flag PIE1bits.RCIE = 1; // Enable UART receive interrupts IPR1bits.RCIP = 0; // Set UART receive as low priority interrupt PIR1bits.TXIF = 0; // Clear TX interrupt flag PIE1bits.TXIE = 0; // Disable UART transmit interrupts IPR1bits.TXIP = 0; // Set UART transmit as low priority interrupt /* Configure the IPEN bit (1=on) in RCON to turn on/off int priorities */ RCONbits.IPEN = 1; /* Enable interrupts */ PIR1 = 0; // Clear peripheral interrupts INTCONbits.GIEH = 1; // Enable high priority interrupts INTCONbits.GIEL = 1; // Enable low priority interrupts }
void configuracao_EUSART (void) { //#define CloseUSART( ) RCSTA&=0b01001111,TXSTAbits.TXEN=0,PIE1&=0b11001111 CloseUSART(); // fecha qualquer USART que estaria supostamente aberta antes // just closes any previous USART open port OpenUSART( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 12 ); // em 4 Mhz, com BRGH LOW (Bit Rate Generator LOW): // 51 = 1200bps; 25 = 2400bps; 6 = 9600bps (Err); // em 8 Mhz, com BRGH LOW (Bit Rate Generator LOW): // 103 = 1200bps; 25 = 4800bps; 51 = 2400bps; 12 = 9600bps }
void usart_init(void) { unsigned char config=0,spbrg=0; //status light on TRISA = 0; //set as output PORTAbits.RA4 = 0; //turn on CloseUSART(); //turn off usart if was previously on // configure USART config = USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH; // 9600 = 10 MHz / (4 * (spbrg + 1)) // spbrg = (10 MHz/9600*4) - 1 spbrg = (10000000/9600*4) - 1; OpenUSART(config, spbrg); }
void main(void) { char mensagem[]="Pronto > "; ADCON1=0xF; // torna todas portas AN0 a AN12 como digitais // na PIC18F2525 somente AN0 a AN4 e AN8 a AN12 // nas PICs com 40 pinos, todos ANs de 0 a 12 // // PCFG3:PCFG0: A/D Port Configuration Control bits // Note 1: // The POR value of the PCFG bits depends on the value of // the PBADEN Configuration bit. When PBADEN = 1, // PCFG<2:0> = 000; when PBADEN = 0, PCFG<2:0> = 111. TRISB=0; // output do LCD na PORTB / LCD output in PORTB initLCD(); // inicia comandos de configuracao do LCD // LCD init commands /* * obs: a biblioteca XLCD.H da PLIB para PIC18, considera como default o LCD * conectado na PORTB com as seguintes pinagens: * Lower Nibble = LCD DATA in PORT B0, B1, B2, B3 (DATA_PORT) RW_PIN in B6 ( PORT for LCD RW , can also be grounded) RS_PIN in B5 ( PORT for LCD RS ) E_PIN in B4 ( PORT for LCD Enable Pin ) */ TRISA2=1; // entrada do BOTAO / push-BOTTON input TRISC0=0; // led verde 1 TRISC1=0; // led verde 2 TRISC2=0; // led verde 3 TRISC3=0; // led verde 4 TRISC4=0; //buzzer TRISC5=0; //led vermelho LED_VERMELHO=1; LED1=1; Delay10KTCYx(1000); while(BusyXLCD()); WriteCmdXLCD(0x01); // comando para limpar LCD / command to clear LCD while(BusyXLCD()); putrsXLCD ("Serial IO EUSART"); // somente logotipo / just a start logo SetDDRamAddr(0x40); // Linha 2 do Display LCD / second line of LCD while(BusyXLCD()); putrsXLCD ("RS232 PIC18F2525"); // somente logotipo / just a start logo SetDDRamAddr(0x00); // volta cursor para linha 0 e posicao 0 do LCD // put cursor in position 0,0 of LCD contadorDisplay=0; // zerando a posicao de caracteres do LCD // reseting the LCD character count // Habilitacao dos pinos C6 e C7 para uso da EUSART (explicacao abaixo): // Enabling pins C6 and C7 for EUSART (explanation bellow): TRISC6=1; TRISC7=1; RCSTAbits.SPEN=1; /* The pins of the Enhanced USART are multiplexed with PORTC. In order to configure RC6/TX/CK and RC7/RX/DT as a USART: ? SPEN bit (RCSTA<7>) must be set (= 1) ? TRISC<7> bit must be set (= 1) ? TRISC<6> bit must be set (= 1) Note: The EUSART control will automatically reconfigure the pin from input to output as needed. */ LED1=1; LED2=0; LED_VERMELHO=0; CloseUSART(); // fecha qualquer USART que estaria supostamente aberta antes // just closes any previous USART open port Delay10KTCYx(1); //Passing 0 (zero) results in a delay of 2,560,000 cycles Delay10KTCYx(1000); //PORTC=1; Delay10TCYx(50); PORTC=0; OpenUSART( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51 ); // Baud Rate "51" para 2400bps @ 8mhz em modo assincrono // de acordo com DS39626E-page 207 do Datasheet em PDF da PIC18F2525 // These are common comands for 2400 bps running at 8 mhz, assyncronous mode // just as being showed in PIC18F2525 Datasheet (DS39626E-page 207) //baudUSART (BAUD_8_BIT_RATE | BAUD_AUTO_OFF); // page 1158 do pic18_plib.pdf (capitulo 8.17.1.3.3 baud_USART) // Set the baud rate configuration bits for enhanced usart operation // These functions are only available for processors with // enhanced usart capability (EUSART) /* The Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) module is one of the two serial I/O modules. (Generically, the USART is also known as a Serial Communications Interface or SCI.) The EUSART can be configured as a full-duplex asynchronous system that can communicate with peripheral devices, such as CRT terminals and personal computers. It can also be configured as a half- duplex synchronous system that can communicate with peripheral devices, such as A/D or D/A integrated circuits, serial EEPROMs, etc. The Enhanced USART module implements additional features, including automatic baud rate detection and calibration, automatic wake-up on Sync Break recep- tion and 12-bit Break character transmit. These make it ideally suited for use in Local Interconnect Network bus (LIN bus) systems. (DS39626E-page 201) */ INTCONbits.PEIE = 1; // interrupcoes para perifericos INTCONbits.GIE = 1; // interrupcoes globais while(BusyUSART()); putrsUSART("\n\rLed1 e LedVermelho = Interrupcao; Led2 = while wait; Led3/4 = Err"); while(BusyUSART()); putrsUSART("\n\rComandos ^E (echo), ^P (lcd), ^L (cls), ^S (status)"); while(BusyUSART()); putrsUSART("\n\rTerminal Serial: "); while(BusyUSART()); putsUSART(mensagem); LED_VERMELHO=0; LED1=1; LED2=1; //LED4=1; while (1){ //LED4=RCIF; // o LED4 mostra o status da Interrupcao da RX Serial // Led4 shows the status of RX interrupt //LED3=TXIF; // o LED3 mostra o status da suposta interrupcao de TX // Led3 shows the suposed TX interrupt state LED3=OERR; LED4=FERR; /* bit 2 FERR: Framing Error bit 1 = Framing error (can be cleared by reading RCREG register and receiving next valid byte) 0 = No framing error * bit 1 OERR: Overrun Error bit 1 = Overrun error (can be cleared by clearing bit, CREN) 0 = No overrun error */ LED2=~LED2; Delay10KTCYx(10); // quando esta no modo de espera de tecla* (interrupcao), fica piscando // os tres leds para demonstrar a despreocupacao do loop while // *na verdade essa espera de tecla eh o RX da serial // when being in wait mode (for interrupt *keypress )just flashes the tree // leds to demonstrate the un-commitment of while loop to keypress // * this keypress event is the serial RX // Check for overrun error condition if (OERR == 1) { // Clear the overrun error condition BUZZ=1; CREN = 0; CREN = 1; Delay100TCYx(10); BUZZ=0; } LED_VERMELHO = RCIF; // buffer de recepcao cheio } return; }