void main(void)
{
Configurate_USART();
char Temp_Instruction;
while(1)
{
if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set
{
Temp_Instruction = RCREG;
if(Temp_Instruction == 'a')
{
LATBbits.LATB0 = 1; // RB-0 to High
LATBbits.LATB1 = 1; // RB-1 to High
RCREG = 0; //Empty register
}
if(Temp_Instruction == 'b')
{
LATBbits.LATB0 = 0; // RB-0 to Low
LATBbits.LATB1 = 0; // RB-1 to Low
RCREG = 0; //Empty register
}
}
}
}
I need to know what errors you are getting....
What compiler are you using I don't recognise " Configurate_USART();"...
void Configurate_USART (void)
{
//LEDs
TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.
// USART configuration
TXSTAbits.TX9 = 0; // 8-bit transmission
TXSTAbits.TXEN = 1; // Transmit enabled
TXSTAbits.SYNC = 0; // Asynchronous mode
TXSTAbits.BRGH = 1; // High speed
RCSTAbits.SPEN = 1; // Enable serial port - configures RX/DT and TX/CK pins as serial port pins
RCSTAbits.RX9 = 0; // 8-bit reception
RCSTAbits.CREN = 1; // Enable receiver
BAUDCONbits.BRG16 = 0; // 8-bit baud rate generator
SPBRGH = '129'; //calculated
TRISCbits.RC6 = 0; // make the TX pin a digital output
TRISCbits.RC7 = 1; // make the RX pin a digital input
}
Are we to assume you have programmed the USART before and know it to be working (i.e. SPBRGH:SPBRG, SYNC bit, SPEN bit,RXDTP bit, RCIE bit, RX9 bit, CREN bit). You can easily test the USART is configured properly by writing back to the USART TX what you received on the RX.
You could use any terminal program (hyperterm, terraterm, procom, etc). The simplest way to start is by connecting the TX and RX pins on the cable. The terminal program would then echo back the characters you typed. Next you remove this connection on the cable and connect the cable to the PIC. You then write your PIC program so it echoes the received data back to transmit pin. You know you have everything configured properly on the PIC when you receive back what you typed.
You could use any terminal program (hyperterm, terraterm, procom, etc). The simplest way to start is by connecting the TX and RX pins on the cable. The terminal program would then echo back the characters you typed. Next you remove this connection on the cable and connect the cable to the PIC. You then write your PIC program so it echoes the received data back to transmit pin. You know you have everything configured properly on the PIC when you receive back what you typed.
Thanks for your replies. I did install hyperterm, but cannot really understand how to use it. Do I sent data from hyperterm itself or from Visual Studio? When I pressed SEND from hyperterm, a window popped up telling me to select a file. Isn't it just characters that you send?Yes, your overall idea on how to give the instructions to the PIC based on received USART data is ok. But you first need to make sure the PIC is listening. If you can successfully loop back the received data, and send it back to the terminal you know the USART setup is correct; and then your only concern is really what you want to do. But it looks like at this point you can't say for sure your USART setup is correct.
more luck
Yes, how deep you want to go? Do you take the red pill, or the blue pill?Also, is there a neater way to to this?
// INCLUDE PIC18F4550 HEADERS (LIBRARIES) -------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<p18f4550.h>
#include<delays.h> //Library for delays IMPORTANT TO INCLUDE THIS LIBRARY FOR DELAYS ()()()()()()()()()()()()()()()()()()()()()()()()()()
// COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma config FOSC = HSPLL_HS // Since a 20MHz Xtal is used, set FOSC to HSPLL_HS (HS stands for high speed)
#pragma config PLLDIV = 5 // Divide the 20MHz by 5 to provide 4 MHz input to the 96 MHz PLL
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
#pragma config USBDIV = 2 // USB clock coming from 96 MHz PLL output is divided by 2
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled (this is used to stop operation when frequency gets out of tollerance--usually used for medical applications)
#pragma config IESO = OFF // Oscillator Switchover mode disabled
#pragma config PWRT = OFF // Enable Power-up timer (This causes the PIC to delay until full operating voltage is reached before the program starts)
#pragma config BOR = ON // Enable Brown-out reset (This automatically put the PIC in reset if the power coming into the chip isn't sufficient to work reliably, hence preventing malfunctions)
#pragma config VREGEN = ON // Use internal USB voltage regulator from 5V to 3.3V
#pragma config WDT = OFF // Disable Watchdog timer (This is used so when the system see that it may not be working as expected, it automatically resets the PIC)
#pragma config MCLRE = ON // Enable MCLR Enable (MCLR is pin 1 of the PIC. When ground is applied it resets the PIC)
#pragma config LVP = OFF // Disable low voltage ICSP (This is another way to program the PIC. Since not used - Disable)
#pragma config ICPRT = OFF // Disable dedicated programming port (44-pin devices) (This is a Dedicated In-Circuit Debug/Programming Port)
#pragma config CP0 = OFF // Disable code protection (Used to protect the program from being coppied)
// INITIALIZE FUNCTIONS (Prototyping the functions) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
// MAIN FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void main(void)
{
TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.
TRISCbits.RC6 = 0; // make the TX pin a digital output
TRISCbits.RC7 = 1; // make the RX pin a digital input
SPBRG = 25; // "25" calculated using formula from table 20-1 in the PIC18F4550 datasheet
RCSTAbits.SPEN = 1; // enable serial port (configures RX/DT and TX/CK pins as serial port pins)
TXSTAbits.TXEN = 1; // transmit enabled
RCSTAbits.CREN = 1; // enable receiver
LATBbits.LATB0 = 0; // RB-0 to High //turn LEDs of on start up
LATBbits.LATB1 = 0; // RB-1 to High
while(1)
{
if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
{
if(PIR1bits.TXIF == 1) // check if the TXREG is empty
{
TXREG = RCREG; // echo received data back to sender
}
}
}
}
// INCLUDE PIC18F4550 HEADERS (LIBRARIES) -------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<p18f4550.h>
#include<delays.h> //Library for delays IMPORTANT TO INCLUDE THIS LIBRARY FOR DELAYS ()()()()()()()()()()()()()()()()()()()()()()()()()()
// COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma config FOSC = HSPLL_HS // Since a 20MHz Xtal is used, set FOSC to HSPLL_HS (HS stands for high speed)
#pragma config PLLDIV = 5 // Divide the 20MHz by 5 to provide 4 MHz input to the 96 MHz PLL
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
#pragma config USBDIV = 2 // USB clock coming from 96 MHz PLL output is divided by 2
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled (this is used to stop operation when frequency gets out of tollerance--usually used for medical applications)
#pragma config IESO = OFF // Oscillator Switchover mode disabled
#pragma config PWRT = OFF // Enable Power-up timer (This causes the PIC to delay until full operating voltage is reached before the program starts)
#pragma config BOR = ON // Enable Brown-out reset (This automatically put the PIC in reset if the power coming into the chip isn't sufficient to work reliably, hence preventing malfunctions)
#pragma config VREGEN = ON // Use internal USB voltage regulator from 5V to 3.3V
#pragma config WDT = OFF // Disable Watchdog timer (This is used so when the system see that it may not be working as expected, it automatically resets the PIC)
#pragma config MCLRE = ON // Enable MCLR Enable (MCLR is pin 1 of the PIC. When ground is applied it resets the PIC)
#pragma config LVP = OFF // Disable low voltage ICSP (This is another way to program the PIC. Since not used - Disable)
#pragma config ICPRT = OFF // Disable dedicated programming port (44-pin devices) (This is a Dedicated In-Circuit Debug/Programming Port)
#pragma config CP0 = OFF // Disable code protection (Used to protect the program from being coppied)
// INITIALIZE FUNCTIONS (Prototyping the functions) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
// MAIN FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void main(void)
{
TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.
TRISCbits.RC6 = 0; // make the TX pin a digital output
TRISCbits.RC7 = 1; // make the RX pin a digital input
SPBRG = 25; // "25" calculated using formula from table 20-1 in the PIC18F4550 datasheet
RCSTAbits.SPEN = 1; // enable serial port (configures RX/DT and TX/CK pins as serial port pins)
TXSTAbits.TXEN = 1; // transmit enabled
RCSTAbits.CREN = 1; // enable receiver
LATBbits.LATB0 = 0; // RB-0 to High //turn LEDs of on start up
LATBbits.LATB1 = 0; // RB-1 to High
while(1)
{
if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
{
if(PIR1bits.TXIF == 1) // check if the TXREG is empty
{
TXREG = RCREG; // echo received data back to sender
}
}
}
}
if(PIR1bits.TXIF == 1) // check if the TXREG is empty
while(!TXSTAbits.TRMT );// Wait until empty
TXREG = RCREG;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?