 |
 |
| View previous topic :: View next topic |
| Author |
Message |
AlbertCharles
Joined: 15 Sep 2023 Posts: 17
|
| Read from UART at 115200 pic16f88 |
Posted: Sat Jan 03, 2026 6:53 pm |
|
|
| Code: | //============PROGRAM 1 ================================
//Hi guys, i need to read high and low pulse from a pulse generator circuit....
//Thats works! Get_Timer1() capture High and Low times (in us) correctly!
// But is necessary to change the third directive baudrate to 115200.
//So, i changed delay clock to 20MHZ and fuses INTRC_IO to HS.
// After that, Get_Timer1() captures High and Low pulses incorrectly!
// Why?? Mny thnks ALL
#include <16F88.h>
#fuses NOWDT,protect,PUT,NOLVP, NOMCLR,NOBROWNOUT,INTRC_IO ,CCPB3 // support: cmd reset, cmd factory reset,
#use delay(clock= 8MHZ) // test with 20MHZ, go to work com 115200 bps <=== !!!
#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_B7,stop=1,parity=n,bits=8,errors,stream=UART3) //<==?????
#use rs232(baud=9600, xmit=PIN_A4, rcv=PIN_B4,stop=1,parity=n,bits=8,errors,stream=UART2) // fputc (UART2,UART3))
#use rs232(baud=9600, xmit=PIN_b5, rcv=PIN_B2,stop=1,parity=n,bits=8,errors) // putc (UART-TxB5, RxB2)-canal
#use TIMER(TIMER=1,TICK=1us,BITS=32,NOISR)
#define Sinal_1 PIN_B0 // pin 6 FROM PULSE GENERATOR....
unsigned int16 High=0,Low=0 ;
short GetSignal()
{
SET_TIMER1(0);
while(input(Sinal_1)) // Check sends logic HIGH, se input is 1)
{
High = GET_TIMER1();
}
SET_TIMER1(0);
while(!input(Sinal_1)) // Check sends logic LOW)
{
Low = GET_TIMER1();
}
if( ((High > 600) && (High < 1450)) && ((Low > 10000) && (Low < 13000)) )
{
return TRUE;
}
else
{
return FALSE;
}
}
void main()
{
setup_oscillator(OSC_INTRC | OSC_8MHZ);
output_a(0);
set_tris_a(0b00001111);// Configure RA5 pin as output and others as inputs
clear_interrupt(INT_TIMER1); // Clear timer0 interrupt flag bit
fprintf(UART2,"\nStartInicio\n\n");
while(TRUE)
{
if (GetSignal()=true) fprintf(UART2,"\nHIGH OK\n\n"); else fprintf(UART2,"\nLOW OK\n\n");
}
} |
Last edited by AlbertCharles on Sun Jan 04, 2026 2:18 pm; edited 3 times in total |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9611 Location: Greensville,Ontario
|
|
Posted: Sun Jan 04, 2026 1:34 pm |
|
|
| hmm... UART2 is a bit banged UART( SW not the internal real UART) ,might be the issue ? |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 17
|
|
Posted: Sun Jan 04, 2026 2:12 pm |
|
|
| temtronic wrote: | | hmm... UART2 is a bit banged UART( SW not the internal real UART) ,might be the issue ? |
HI..thnks... as i said: The UART i need to send on the 115200, is only the UART1(HW). The others(UART2,UART3-SW) can stay in 9600.
The PIC16F88 only communicates at 115200 if the clock is 20MHz (HS); however, when changing from 8MHz to this frequency, the time base of the timer reading get_timer (in µs) changes. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9611 Location: Greensville,Ontario
|
|
Posted: Sun Jan 04, 2026 2:55 pm |
|
|
you'll need to recode for the timer1 'reload' value or whatever it's called.
have never used '#use timer(........... ) too old( did PIC ASm for a decade..) for fancy C stuff, always hand code and check register to confirm the bits are correct.
check the listing ! it may be a compiler 'bug' isn't setting stuff up correctly. |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 17
|
|
Posted: Mon Jan 05, 2026 4:53 am |
|
|
| Hi TT... Thanks... Using the PIC16F88 keeps the project outdated, but we manufactured some PCBs, so I need to use them because the layout is for this device. However, we have a new design, replacing it with the PIC18F26K22. But today, I still need to use the PIC16F88. I have been trying for a long time to change the Timer settings to reach the get Timer value for reading in microseconds correctly...(For 20mhz clock) but it hasn’t worked. Can you suggest, at the code level, some practical changes? |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 353
|
|
Posted: Mon Jan 05, 2026 9:14 am |
|
|
At 8MHz clock, instruction = 2MHz.
For timer1 = 1us resolution then divide by 2 prescale.
At 20MHz clock, instruction = 5MHz.
for timer1 = 1us resolution then divide by 5 prescale but options are only 1, 2, 4, or 8.
Try using a 16MHz crystal instead. |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 17
|
|
Posted: Mon Jan 05, 2026 9:44 am |
|
|
| gaugeguy wrote: | At 8MHz clock, instruction = 2MHz.
For timer1 = 1us resolution then divide by 2 prescale.
At 20MHz clock, instruction = 5MHz.
for timer1 = 1us resolution then divide by 5 prescale but options are only 1, 2, 4, or 8.
Try using a 16MHz crystal instead. |
Hi Gg.. Thanks... I´ll try today..and i´ll post result here.. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|