View previous topic :: View next topic |
Author |
Message |
filjoa
Joined: 04 May 2008 Posts: 260
|
16f628A RS232 problem |
Posted: Tue Oct 12, 2010 2:42 pm |
|
|
Hi
My program work fine with PIC18F252 but when I try transfer and program from 18F252 to a 16F628A, after I change it don't work.
I think which I only need change 18F252.h to 16F628A.h.
My problem now is because INT_RDA don't work. PIC16F628A don't receive in my case '#'.
Someone can help?
Code: |
#include <16f628a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)
#include <stdlib.h>
// ### VARIAVEIS GLOBAIS ###
int16 SPEED = 1600;
int16 STEERING = 1400;
int1 data_available = FALSE;
char c;
// ### READ RS232 ###
#int_rda
void rda_isr(void)
{
c = getc();
data_available = TRUE;
}
// ### SPEED CONTROL ###
#int_TIMER1 //every 1.6ms
void TIMER1_isr(void)
{
output_high(PIN_B4);
delay_us(SPEED);
output_low(PIN_B4);
}
// ### STEERING CONTROL ###
#int_TIMER2 //every 1.4ms
void TIMER2isr(void)
{
output_high(PIN_B5);
delay_us(STEERING);
output_low(PIN_B5);
}
void read_str()
{
int8 i=0;
char string[11];
BYTE sp[5];
BYTE dr[5];
do {
while (data_available == FALSE);
data_available = FALSE;
string[i]=c;
i++;
} while (c != 13); // '\n'
for (i=0; i<=3; i++)
{
sp[i]=string[i+1];
}
for (i=0;i<=3;i++)
{
dr[i]=string[i+6];
}
SPEED = atol(sp);
STEERING = atol(dr);
SPEED=1100 + SPEED; // gama de 1200 - 1600 - 2000
STEERING=900 + STEERING; // gama de 1000 - 1400 - 2000
}
void main (void)
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // periodo de 13.1ms
enable_interrupts(INT_TIMER1);
setup_timer_2(T2_DIV_BY_16,243,16); // periodo de 12.4ms
enable_interrupts(INT_TIMER2);
while (1)
{
while (c!='#');
if (data_available)
{
data_available=FALSE;
if (c=='#') read_str();
}
}
}
|
PS: I try put "printf("char #")" after "while(c!='#')" to test my entry but my program never pass this step.
best regards |
|
|
prwatCCS
Joined: 10 Dec 2003 Posts: 70 Location: West Sussex, UK
|
|
Posted: Tue Oct 12, 2010 3:02 pm |
|
|
Apologies if this seems a bit off-topic.
I notice in your Timer1 and Timer2 interrupt code you call delay_us(), and by the looks of your arguments, may be expecting delays of over 1ms.
Given that your comment suggests these irq's are meant to occur every 1.6mS and 1.4ms, you dont leave much time for your poor little 16F628A to do much else.
As a matter of principle, I would always try very very hard to avoid delay loops inside irq handlers. The design goal should always be "get in fast, get done fast, get out fast".
You will find that the irq overheads on the 16F part are probably longer thatn when using the 18F parts, and you have run out of steam ....
Better to use the timers to generate another irq at the end of the required timeout period. _________________ Peter Willis
Development Director
Howard Eaton Lighting Ltd UK |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Tue Oct 12, 2010 3:13 pm |
|
|
hi
You mean that the reading time is diferent the 16F628A and 18F252? I do not think has much meaning, or at least I do not understand where's the difference ... because I think the irq events are parallel to the operation of my program and should not interfere because the delay is much smaller than the time of irq or am Engand?
I just used delay_us () inside the irq because I want to generate a square wave and not see another way to do it.
best regards |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Tue Oct 12, 2010 4:30 pm |
|
|
Hi
is possible problem stay there?
16f628a --> CPU Speed (MIPS) = 5
18f252 --> CPU Speed (MIPS) = 10 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 12, 2010 4:59 pm |
|
|
Just make a test program, such as I show in this thread, and find
the problem. In this thread he also has large complicated program
and then he tries my small test program and that allows him to
find the problem.
http://www.ccsinfo.com/forum/viewtopic.php?t=37869 |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Wed Oct 13, 2010 8:43 am |
|
|
hi
One problem solved... problem stay on hardware... and with this debug program I solve it... thanks
With this program I try read from PC and string with this format "#sxxxdxxx\n" where xxxx is a number (0000 to 1024).
But my program don't read this...
I try this:
Code: |
while (1)
{
while (c!='#');
if (data_available)
{
data_available=FALSE;
if (c=='#') read_str();
}
printf("SP: %lu \n\r",SPEED);
printf("DR: %lu \b\r", STEERING);
}
|
and with this test my I see which my program don't wait for "#", on hyperterminal it write constantly "SP: 1600" and "DR: 1600 don't stop...
Someone know why?
PS: with PIC18F252 it don't happen...
best regards |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Wed Oct 13, 2010 5:59 pm |
|
|
hi
is possible this problem is cause because MIPS? or have other reason?
best regards |
|
|
|