View previous topic :: View next topic |
Author |
Message |
harith
Joined: 23 Jun 2014 Posts: 8 Location: Srilanka
|
Serial communication PC and PIC16F876A |
Posted: Sat Jun 28, 2014 2:03 am |
|
|
Need Help....
I tried to communicate with my laptop by connecting USB to Serial converter(i checked it.it's working) and my demo board for 16F876A..
there i used MAX232...
im using CCS C compiler to coding.....
i need to send some data to PIC and display it on LCD display and reply to PC....
but after getc or gets command it is not coming to next line....
why is that????
LCD is working before that command...
i tried it in many ways but cant find what is the wrong... pls help me...
this is part a of code i used...
Code: | {
char string[30];
gets(string); // read string from serial
printf(lcd_putc,"\f"); // clear lcd
printf(lcd_putc,"%c",string); // print string in LCD
puts(string); // write string to serial
}
|
pls help me... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Jun 28, 2014 4:47 am |
|
|
without seeing your complete program odds are real good you need to add 'errors' to the use rs232(...options....) function
without it, the hardware UART will lockup due to 'overrun' condition.
If this doesn't fix it, then please post entire program so we can look at it.
hth
jay |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sat Jun 28, 2014 5:13 am |
|
|
Hi,
Read the CCS manual to verify which format specifier you should use with printf to work with a string.....
John |
|
|
harith
Joined: 23 Jun 2014 Posts: 8 Location: Srilanka
|
full code.... |
Posted: Sat Jun 28, 2014 11:57 pm |
|
|
Thank you for reply.
This is my full code. I tried it in many ways
with serial interrupt, in main function but not working.
I don't know whether it need to change the banksel this compiler like in assembly.
Code: | #include <16F876A.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(crystal=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)
#include <flex_lcd.c>
char string[30];
//------------------Serial data receive interrupt-------------------------------------------------
#INT_RDA
void RDA_isr(void)
{
/*
//ch = getc();
//putc(ch);
*/
gets(string);
puts(string);
printf(lcd_putc,"\f");
printf(lcd_putc,"%c",string);
}
//-----------------------------------------------------------------------------------
void main()
{
lcd_init() ;
enable_interrupts(INT_RB);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(TRUE)
{
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Sun Jun 29, 2014 1:16 am |
|
|
Multiple things:
1) You must _never_ have an interrupt enabled without a handler present. You have INT_RB enabled, but no code to handle it. This will hang the chip.
2) INT_RDA, says 'one character is ready to be read'. You should not ever use 'gets' in INT_RDA. Just read the _one_ character. Look at ex_sisr for an example of the code required.
3) Interrupts must in general never take longer to execute than the time between interrupt 'events'. So things like printf, inside an interrupt _will_ cause problems.
4) A string is not a character. %c, is not the printf specifier to print a string.
5) You have a stream specifier in the UART setup, then don't use streams.
Poor (quickly done) 're-sculpt'.
Code: |
#include <16F876A.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(crystal=8000000)
#use rs232(baud=9600,parity=N,UART1bits=8,ERRORS)
#include <flex_lcd.c>
#define BUFF_LEN 30
char string[BUFF_LEN];
int1 have_string=FALSE;
//------------------Serial data receive interrupt-------------------------------------------------
#INT_RDA
void RDA_isr(void)
{
static int ctr=0;
int ch;
ch=getc();
if (ch=='\n')
{
string[ctr]='\0';
ctr=0;
have_string=TRUE;
return;
}
string[ctr++]=ch;
if (ctr==BUFF_LEN)
ctr--; //throw away characters if buffer overflows
}
//-----------------------------------------------------------------------------------
void main()
{
delay_ms(500); //many LCD's require longer to wake
lcd_init() ;
//enable_interrupts(INT_RB);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(TRUE)
{
if (have_string)
{
have_string=FALSE;
printf(lcd_putc,"\f%s",string);
}
}
}
|
This uses a 'linear' buffer instead of the circular buffer in ex_sisr, and generates the null terminator to make a string, when the line feed is seen. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sun Jun 29, 2014 4:26 am |
|
|
Hi,
Yeah, you "tried in many ways" except apparently to actually read the advice that was offered to you!! The only thing that was really wrong with your original code was the printf specifier. Ttelmah pointed this out again as #4. I told you this problem but you ignored the advise. You also ignored temtronics advise to add 'Errors' to your #use rs232 directive..... Why did you do that?
John
Last edited by ezflyr on Sun Jun 29, 2014 8:46 pm; edited 1 time in total |
|
|
harith
Joined: 23 Jun 2014 Posts: 8 Location: Srilanka
|
Thank you... |
Posted: Sun Jun 29, 2014 6:44 pm |
|
|
Mr.John sorry.. im just a starter of this PIC C.. its not like that... i didn't ignore it...i posted the code which i have tried before your replys.. i didn't know how to add errors to rs232...now i know that...without your advices.. i will loss my way..
Mr.Ttelmah Thank you very much for your kindly advices..i didn't know that things..i tried to print a character also but not in this way sir .ill try it..and let u know...thank you again!
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jun 30, 2014 3:38 pm |
|
|
Quote: | I tried to communicate with my laptop by connecting USB to Serial converter(i checked it.it's working) and my demo board for 16F876A..
there i used MAX232... |
As I understand the text above, your hardware *might* be wrong.
IF you have a USB to Serial adapter (I am assuming FTDI232).
AND you connected a MAX232 between the pic and the FTDI Chip...
Thats not going to work.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Jun 30, 2014 6:18 pm |
|
|
Hi Gabriel,
The consumer 'USB-to-serial' adapters tend to be RS232 levels, while the ones sold to hobbyists, etc. tend to be TTL levels. The OP would have to post a link to be sure!
John |
|
|
harith
Joined: 23 Jun 2014 Posts: 8 Location: Srilanka
|
checked it... |
Posted: Sat Jul 05, 2014 3:26 am |
|
|
im using that USB to TTL Converter IC CP2102..
i checked it..it is working...
some times when i compile the code it shows that "Interrupt disabled during call to prevent re-entrancy:[@delay_ms1],[lcd_send_nibble],[lcd_send_byte].[lcd_putc]...."
why it is...
it seems like when serial interrupt occur it not returning to main function... why is it..how can i take it back to main function...
how can i solve this please any one.. help me... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Jul 05, 2014 5:37 am |
|
|
Every time the PIC is executing a 'delay' NOTHING else can occour. Nature of the delay code. It's only a warning to you, the code will run though.
The PIC sits in a tight loop to create the delay.
There are ways to get around the 'warning' but probably are not worth your time and efforts as it uses timers, ISRs, etc.
Others may reply with more details or alternatives.
Generally speaking it's 'bad programming' to use 'hard coded delays'.
If you're still having problems , post your most recent version of code.There may be something else we can help you with.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Sat Jul 05, 2014 9:20 am |
|
|
It is actually saying one other thing.
That he is trying to use the LCD, both inside an ISR, and outside.
Don't....
Thinks about it. The LCD is one device. Imagine you start sending a byte to it in the main code. Then what happens if the ISR also tries to send a byte?....
The compiler is trying to protect the poster from this disaster.
Also though the LCD is _slow_ as such using it in an ISR will lead to other problems. |
|
|
|