![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
asadi.siyavash@gmail.com
Joined: 22 Aug 2013 Posts: 22
|
rs232 not working with keypad |
Posted: Fri Aug 23, 2013 8:30 am |
|
|
Hi all,
I am really confused please help me, it is very simple but I can't solve it.
when I use keypad with timer & etc it work very well but when I use RS232 and I write getc(); it doesn't work! I am really confuse now please help me.
Code: |
#include <18F452.h>
#fuses EC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//=============================
//Keypad connection:
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3
// Keypad layout:
char const KEYS[4][4] =
{{'7','4','1','P'},
{'8','5','2','0'},
{'9','6','3','E'},
{'A','B','C','M'}};
#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second
void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
port_b_pullups(true);
}
short int ALL_ROWS (void)
{
if(input (row0) & input (row1) & input (row2) & input (row3))
return (0);
else
return (1);
}
int8 keypad()
{
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;
byte kchar;
byte row;
kchar='\0';
if(++kbd_call_count>KBD_DEBOUNCE_FACTOR)
{
switch (col)
{
case 0:
output_low(col0);
output_high(col1);
output_high(col2);
output_high(col3);
break;
case 1:
output_high(col0);
output_low(col1);
output_high(col2);
output_high(col3);
break;
case 2:
output_high(col0);
output_high(col1);
output_low(col2);
output_high(col3);
break;
case 3:
output_high(col0);
output_high(col1);
output_high(col2);
output_low(col3);
break;
}
if(kbd_down)
{
if(!ALL_ROWS())
{
kbd_down=false;
kchar=last_key;
last_key='\0';
}
}
else
{
if(ALL_ROWS())
{
if(!input (row0))
row=0;
else if(!input (row1))
row=1;
else if(!input (row2))
row=2;
else if(!input (row3))
row=3;
last_key =KEYS[row][col];
kbd_down = true;
}
else
{
++col;
if(col==4)
col=0;
}
}
kbd_call_count=0;
}
return(kchar);
}
//===========================
void main()
{
char k;
char ch;
kbd_init();
printf("\r\Starting ...");
//ch= getc(); when I uncomment this line the key pad doesn't work
while(TRUE)
{
k=keypad();
if(k!=0)
{
if(k=='*')
printf("%c", '*');
else
printf("%c", k);
}
}
} |
when I remove comment of ch=getc(); the keypad doesn't work.
please help me I need to get data with rs232.
thank you alot.
Sincerely yours. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Fri Aug 23, 2013 8:51 am |
|
|
this line ...
ch= getc();
...will ONLY be satisfied WHEN a character comes in the UART
so your program 'stalls' or waits until something comes in.
what you should do is look at the 'ex_sisr.c' program in the examples folder that CCS supplies. It's an interrupt driven program that 'flags' your main program that 'something' has come in and placed it into a buffer.This way your 'main' program can do 'something' without waiting ( stalling) for serial data to come.
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
asadi.siyavash@gmail.com
Joined: 22 Aug 2013 Posts: 22
|
|
Posted: Fri Aug 23, 2013 11:54 pm |
|
|
thank you temtronic,
my first problem solved with your help, I have two rs232 so I should have two interrupt so i have to use another chip that have 2 hardware UART. because in software UART we don't have interrupt. is it true?
thank you again, |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Sat Aug 24, 2013 12:54 am |
|
|
Correct.
Also the software UART is half duplex (only send _or_ receive at any time), so if anything is coming 'unexpectedly', it'll give problems.
There are software solutions to partially bridge the gap (I have posted a timer interrupt driven RS232 TX here in the past), and you can generate RX using an interrupt pin, but all will involve some form of compromise.
Personally, I'm tending to use the 18F46J50 family for a lot of things at the moment. Has a lot of peripherals and a lot of memory. 2UART's, 2MSSP, USB, 2PWM, CTMU, RTCC, 2Comparators etc. etc.. Up to 13bit ADC's (on the 44J version), and very low power consumption. Unlike most of the older USB chips there is enough memory to have the USB code, and a reasonable amount of other code. The extra peripherals are also 'relocatable' onto different pins.
A bit of searching, should find a chip to suit your requirements.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
asadi.siyavash@gmail.com
Joined: 22 Aug 2013 Posts: 22
|
|
Posted: Sat Aug 24, 2013 6:58 am |
|
|
dear, Ttelmah thank alot for your guide I have 18F46K22 and I should work with it, I simulate it in Proteus it is work well but in real test it doesn't work. I am really confused again, I post my new problem in forum I will be glad to use your knowledge again,
and in the end thanks for your suggestion I'll test 18F46J50 but I bought 18F46k22 and I should work with it,
really thanks for sharing your knowledge ,
Sincerely yours. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|