??? 07/12/08 21:42 Modified: 07/12/08 21:57 Read: times |
#156651 - serial communication |
Hi all,
I am having problem with the following code. This code takes frequency from user and converts the ascii value recieved through serial port into decimal. Problem:-> When I input a frequency between 40000 and 70000 it never enters the first condition. Here is what happens: works perfectly fine for frequency<40000 and above 99000 (it goes into the condition where it should go) but, goes into condition 2nd (>11000 and <40000) for input frequency (>76000 and <99000) and goes in last condtion (else ) for input frequency (>40000 and < =76000). #include <stdio.h> #include <REGX52.h> void frequency_calc ( unsigned char a1,unsigned char b1,unsigned char c1,unsigned char d1,unsigned char e1,unsigned char f1); char uart_data ; sfr p1 = 0x90; sfr p2 = 0xA0; sfr p0 = 0x80; sfr p3 = 0xB0; sbit led1=P1^0; sbit led2=P1^1; sbit led3=P1^2; sbit led4=P1^3; sbit led5=P1^4; sbit led6=P1^5; sbit led7=P1^6; sbit led8=P1^7; sbit led0_0=P0^0; sbit led0_1=P0^1; sbit led0_2=P0^2; sbit led0_3=P0^3; sbit led0_4=P0^4; sbit led0_5=P0^5; int i=1,res_reset=70,res_set=70; unsigned char a=0x00; unsigned char b=0x00; unsigned char c=0x00; unsigned char d=0x00; unsigned char e=0x00; unsigned char f=0x00; unsigned char g=0x00; /** * FUNCTION_PURPOSE: This file set up uart in mode 1 (8 bits uart) with * timer 1 in mode 2 (8 bits auto reload timer). * FUNCTION_INPUTS: void * FUNCTION_OUTPUTS: void */ void main (void) { SCON = 0x50; /* uart in mode 1 (8 bit), REN=1 */ TMOD = TMOD | 0x20 ; /* Timer 1 in mode 2 */ TH1 = 0xFD; /* 9600 Bds at 11.059MHz */ TL1 = 0xFD; /* 9600 Bds at 11.059MHz */ ES = 1; /* Enable serial interrupt*/ EA = 1; /* Enable global interrupt */ TR1 = 1; /* Timer 1 run */ while(1); /* endless */ } void frequency_calc ( unsigned char a1,unsigned char b1,unsigned char c1,unsigned char d1,unsigned char e1,unsigned char f1) { int a_dec=0,b_dec=0,c_dec=0,d_dec=0,e_dec=0,f_dec=0,temp=0,temp1=0,res_set=70,res1_set=70; int double_transitions=0,double_transitions1=0; unsigned long frequency=0; led2=1; /*/this is used for x9315 up/down' ...led2=1 means count up */ p0=0xff; p2=0x00; while (a1 != 0x00) { a1=a1-0x01; a_dec=a_dec+1; } while (b1 != 0x00) { b1=b1-0x01; b_dec=b_dec+1; } while (c1 != 0x00) { c1=c1-0x01; c_dec=c_dec+1; } while (d1 != 0x00) { d1=d1-0x01; d_dec=d_dec+1; } while (e1 != 0x00) { e1=e1-0x01; e_dec=e_dec+1; } while (f1 != 0x00) { f1=f1-0x01; f_dec=f_dec+1; } frequency= (a_dec * 100000) +(b_dec*10000) + (c_dec*1000)+(d_dec*100)+(e_dec*10)+(f_dec); //*******************condition 1***************************** if (frequency>=40000 && frequency<=70000) //condition 1 led0_0=!led0_0 ; //*******************condition 2***************************** else if (frequency>=99000 && frequency<=120000) //condition 2 led0_1=! led0_1; //*******************condition 3***************************** else if ( frequency>=11000 && frequency <=70000) //condition 3 { double_transitions = 2* ((4.548e5/frequency) -9); p2=p2+0x01; while (double_transitions != 0) { double_transitions=double_transitions - 1; led4=!led4; } } //*******************condition 4***************************** else //condtion4 { double_transitions = 2* ((4.548e6/frequency) -37); led0_2=!led0_2; while(res1_set!=0) { led4=!led4; res1_set= res1_set-1; } } } void serial_IT(void) interrupt 4 { if (RI == 1) { /* if reception occur */ //i++; RI = 0; /* clear reception flag for next rec eption */ uart_data = (SBUF - 0x30); /* Read receive data */ if (i==1) {a= (SBUF - 0x30);} else if (i==2) {b= (SBUF - 0x30);} else if (i==3) {c= (SBUF - 0x30); } else if (i==4) {d= (SBUF - 0x30); } else if (i==5) {e= (SBUF - 0x30); } else if (i==6) {f= (SBUF - 0x30); } i++; if (i==7) { i=1; res_reset=70; while(res_reset!=0) { led2=0; led4=!led4; led5=!led5; led6=!led6; led7=!led7; res_reset= res_reset-1; } frequency_calc ( a, b, c, d, e, f); } SBUF = uart_data ; /* Send back same data on uart*/ } else TI = 0; } /* if emission occur */ /* clear emission flag for next emission*/ Thanks for any help |
Topic | Author | Date |
serial communication | 01/01/70 00:00 | |
Have you tried the simulator? | 01/01/70 00:00 | |
Some Comments might help | 01/01/70 00:00 | |
what happened to indentions ??? | 01/01/70 00:00 | |
Readability | 01/01/70 00:00 | |
there is a famous story | 01/01/70 00:00 | |
My head hurts | 01/01/70 00:00 | |
6 Ways to Write (& post) More Comprehensible Code | 01/01/70 00:00 | |
Re: 6 Ways to Write (& post) More Comprehensible | 01/01/70 00:00 | |
a personal opinion | 01/01/70 00:00 | |
Problem aided by bigger screen... | 01/01/70 00:00 | |
key word 'modest' I totally agree | 01/01/70 00:00 | |
Blank Lines: My Opinion | 01/01/70 00:00 | |
Avoided? Feed the wolves with him![]() | 01/01/70 00:00 |