stefan_d6
Joined: 17 Aug 2006 Posts: 6 Location: Varna, Bulgaria
|
|
Posted: Wed Aug 22, 2007 6:26 am |
|
|
I finally solved the problem.
I just changed the baud rate to 19 200 and rs232 works without errors. However I didn't find out way rs232 didn't work correctly with 9600 baud rate. I also removed int_rda and instead I use kbhit() - I think my code allows such construction without any loss of data. Am I right?
Code: | #include <18F1320.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B1, rcv=PIN_B4,ERRORS)
#include <stdlib.h>
#include <input.c>
void main()
{
float rps,rpm;
long turns,duty;
char cmd,no;
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 255, 1); //The cycle time will be (1/clock)*4*t2div*(period+1)
// tcycle = (1/20000000)*4*1*256 = 51.2us -> 19.53125kHz
set_pwm1_duty(0); // duty = tcycle*clock/t2div
// 20% -> 10.24us x 20MHz = 204.8 ~205
// 50% -> 25.6 x 20 = 512
setup_timer_0(RTCC_EXT_L_TO_H);
printf("--- Test program ---");
while(true)
{
if(kbhit())
{
cmd =getc();
switch(cmd)
{
case 'n':set_timer0(0);
delay_ms(1000);
no=getc();
turns=get_timer0();
rps=(float)turns/36;
rpm=rps*60;
printf("\r\n%6.3f",rpm);
break;
case 'd':duty=get_long();
set_pwm1_duty(duty);
printf("\r\nduty = %Lu",duty);
break;
default :printf("\r\nError");
break;
}
}
}
} |
|
|