|
|
View previous topic :: View next topic |
Author |
Message |
isoment01
Joined: 20 May 2017 Posts: 7 Location: turkey
|
getchar sending char. randomly |
Posted: Thu Jun 29, 2017 2:04 am |
|
|
hello everyone. I have problem with getchar. After loading my codes when the program comes getchar line, getchar sending chars two times. Getchar doesn't do waiting for input char. at that moment, seems there is something its buffer. I don't know how to solve this problem. I checked rs232 data cables and max 232
integrated circuit, there is no problem. I posted screenshot of my serial monitor output and my codes in addition.
Code: | #device PIC16F877A
#include <16f877a.h>
#fuses xt,nowdt,noprotect, nobrownout, nolvp, noput, nowrt, nocpd
#use delay (clock=4000000)
#use fast_io(b)
#use fast_io(c)
#use rs232 (baud=9600,xmit=pin_C6, rcv=pin_C7, parity=N, stop=1, Bits=8)
unsigned int16 sayac = 0;
char islem;
#int_timer1
void timer1_interrupts()
{
set_timer1(64923);
while(sayac>0)
{ output_high(pin_c2);
delay_us(20);
output_low(pin_c2);
sayac--;
}
}
void main()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); //262 ms overflow //65,5 ms overflow
setup_timer_2 (T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
set_tris_b(0x00);
output_b(0x00);
set_tris_c(0x00);
output_c(0x00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_timer1);
//enable_interrupts(int_rda);
printf("\n\r System initializing....\n\r");
printf("\n\r bir islem seciniz...\n\r");
delay_us(50);
set_timer1(64923);
while(TRUE)
{
if(kbhit())
{
delay_ms(100);
islem=getchar();
putc(islem);
if(islem=='r')
{ output_high(pin_c3);// motor aktif,motor enable pin
delay_ms(100);
output_high(pin_c1);// motor yönü belirlendi, motor direction pin
sayac=3200;
printf("right start\r\n");
}
if(islem=='l')
{
output_high(pin_c3);
delay_ms(100);
output_low(pin_c1);
sayac=3200;
printf("left start \n\r");
if(islem=='s')
{
output_low(pin_c3);
sayac=0;
printf("motor stop \n\r");
}
}
}
}
}
|
https://hizliresim.com/4MlGDA _________________ hello everyone ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Jun 29, 2017 2:45 am |
|
|
Getchar is receiving two characters.
You wait 100mSec after you have seen kbhit, before you call getchar.
In that time another character has sometimes arrived.
Remember (for instance), that the enter key on a PC, typically sends both a line feed and a carriage return.
With the huge delays scattered around, more data is sometimes waiting to be handled....
There is also a generic problem with your code, of using delays both inside an interrupt and outside. Your timer interrupt will be disabled in all the external delay calls because of this.
If getchar is receiving random characters, there is noise on your serial RX line somewhere.
Seeing comments about motors, remember motors will generate huge amounts of back-emf, when switched off. This has to be trapped somewhere or it will trigger random behaviour on the attached circuitry. Remove your motors and test with LED's in place of them (with suitable current limiting resistors). If the problems disappear, then they are caused by inadequate circuitry for handling the motors.... |
|
|
isoment01
Joined: 20 May 2017 Posts: 7 Location: turkey
|
|
Posted: Thu Jun 29, 2017 3:52 am |
|
|
Ttelmah wrote: | Getchar is receiving two characters.
You wait 100mSec after you have seen kbhit, before you call getchar.
In that time another character has sometimes arrived.
Remember (for instance), that the enter key on a PC, typically sends both a line feed and a carriage return.
With the huge delays scattered around, more data is sometimes waiting to be handled....
There is also a generic problem with your code, of using delays both inside an interrupt and outside. Your timer interrupt will be disabled in all the external delay calls because of this.
If getchar is receiving random characters, there is noise on your serial RX line somewhere.
Seeing comments about motors, remember motors will generate huge amounts of back-emf, when switched off. This has to be trapped somewhere or it will trigger random behaviour on the attached circuitry. Remove your motors and test with LED's in place of them (with suitable current limiting resistors). If the problems disappear, then they are caused by inadequate circuitry for handling the motors.... |
Hey thanks for your comments. Firstly I am already working with leds now. If i succeed, I'll connect my stepper motor driver and motor.
You're right, yes there is noise at the rx pin of the PIC because i checked other components. How do i fix this noise ? Seems there is hardware problem not software right ? btw
Code: | if(kbhit())
{
/output_toggle(pin_b0);
delay_ms(100);
islem=getchar();
putc(islem);*/ |
I am gonna disable that delay. I got this but i try to do it as simple as my timer int. There is not another delay time into the interrupt fuction.
On the other hand, i thought i didn't use rda int. to get chars. This can be a solution ? _________________ hello everyone ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Jun 29, 2017 4:18 am |
|
|
No.
You have a hardware problem.
The software is also faulty, but the hardware has to be fixed first.
What smoothing do you have on the PIC itself?.
How is your supply generated?.
How are the grounds routed between the devices?.
What charge pump capacitors are you using on the MAX232?.
How is the circuit actually built (PCB/breadboard etc.)?.
How are you connecting the LED's?.
etc. etc.. |
|
|
isoment01
Joined: 20 May 2017 Posts: 7 Location: turkey
|
|
Posted: Thu Jun 29, 2017 4:34 am |
|
|
Ttelmah wrote: | No.
You have a hardware problem.
The software is also faulty, but the hardware has to be fixed first.
What smoothing do you have on the PIC itself?.
How is your supply generated?.
How are the grounds routed between the devices?.
What charge pump capacitors are you using on the MAX232?.
How is the circuit actually built (PCB/breadboard etc.)?.
How are you connecting the LED's?.
etc. etc.. |
Wow i am gonna check all now, thank you. _________________ hello everyone ! |
|
|
|
|
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
|