View previous topic :: View next topic |
Author |
Message |
kobayashi
Joined: 10 Feb 2014 Posts: 3
|
PIC Serial transmission |
Posted: Mon Feb 10, 2014 9:44 am |
|
|
HI I'm using a pic for serial transmission with a bluetooth module. I use 0 as start bit, 1 as stop bit and 8 bits of data and no parity.
But I have to create my own functions and not use the
Code: | #use rs232(ERRORS,baud=9600,parity=n,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=MODEM) |
I'm simulating with hyperterminal just writing characters there and the function I wrote for transmission is working, as I call my function for trans and the fgetc() of the rs232.
The problem is with my receive function its not working and when I use the hyperterminal it just gives random characters....
Hope someone can help me here is my code
Code: |
void main()
{
setup_timer_1(T1_INTERNAL |T1_DIV_BY_1); //
setup_timer_3(T3_INTERNAL |T3_DIV_BY_1); //
setup_ccp2(CCP_COMPARE_INT);
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts(GLOBAL);
while(true){
//UART_t(vUART_rec ());//doesnt work
//fputc(fgetc()); //it works
//UART_t(fgetc()); // it works
fputc(vUART_rec ());//doesnt work
//UART_t(0x6d); //it works
}
///Transmission function
void UART_t(uint8_t dato){
cont_bitsportransmitir=10;
vsci_data=dato;
enable_interrupts(INT_CCP2);
do{
}while (cont_bitsportransmitir>0);
}
#int_ccp2
void isr_ccp2()
{
if(cont_bitsportransmitir==10){
output_low(TXD);// start bit
CCP_2=get_timer1()+1040;
}
else if((cont_bitsportransmitir >1) && (cont_bitsportransmitir<10)){
if(vsci_data & 1) output_high(TXD);
else output_low(TXD);
CCP_2+=1040;
vsci_data>>=1;
}
else{
output_high(TXD);
CCP_2+=1040;
}
cont_bitsportransmitir--;
}
//********************************************** RECEIVE Function
///////////////*****************************************************-----------------------------------------
uint8_t vUART_rec(void){
vsci_estado_rec=10;
do{
}while(RXD==1);
enable_interrupts(INT_CCP1);
do{
}while (vsci_estado_rec>0);
return vsci_RHR;
}
#int_ccp1
void isr_ccp1()
{
if(vsci_estado_rec==10)
{
CCP_1=get_timer3()+520;
}
else if(vsci_estado_rec==9)
{
if (RXD==1){
//fake_start_bit();
}
CCP_1+=1040;
}
else if((vsci_estado_rec >0) && (vsci_estado_rec<9))
{
vsci_RHR>>=1;
if(RXD==1) vsci_RHR|=0x80;//
CCP_1+=1040;
}
else
{
if (RXD==0){
//frame_error();//
}
CCP_1+=520;
}
vsci_estado_rec--;
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Mon Feb 10, 2014 10:08 am |
|
|
Before anyone can help...
we need to know
Which PIC you're using.
Which Bluetoothe adapter you're using. Post a link to it.
Your program is not complete,please post complete program.
Try the CCS supplied 'loopback' example to confirm the PC<>PIC hardware is correct( you'll need a MAX232 or equal).
hth
jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Feb 10, 2014 10:13 am |
|
|
while you are at it
what is your pic clock speed
and how are you setting it ??
which compiler version?
lastly why are you not using CCS serial functions?
your receive method has me scratching my head.
then ther is = uart_t() --HUH ???
your home made ones are inscrutable and i can't see how they can work
at all, or why you would code them in the first instance, rather than use CCS
putc(), printf(), etc |
|
|
mdemuth
Joined: 16 Apr 2007 Posts: 71 Location: Stuttgart, Germany
|
|
Posted: Mon Feb 10, 2014 10:26 am |
|
|
You can only read a character if there is one in the buffer:
So must ensure that!
Either you use the rx irq or you simply poll.
I use constructions like this one:
Code: |
rx_complete=false;
while(rx_complete==false)
{
if (kbhit()) character = fgetc(com1); // incoming character is saved
if (character == '*') rx_buffid = 0; // * resets pointer
else rx_buffid++; // increment buffer
if (rx_buffid >= 3) rx_buffid = 3; // avoid stack overflow
rx_buffer[rx_buffid] = character;
if (character == 0x0d) rx_complete=true; // CR ends transmission
counter16++;
if (bit_test(counter16,13)) output_high(LED_1); // let an LED flash to see that the loop is performed
else output_low(LED_1);
restart_wdt();
} |
After that you can take a look what is in between * and <CR> |
|
|
|