|
|
View previous topic :: View next topic |
Author |
Message |
Chevy64
Joined: 05 Apr 2013 Posts: 7
|
int RDA |
Posted: Thu Mar 31, 2016 1:41 am |
|
|
Hi everybody,
My question could be repeated one if so please accept my apology. I am trying to send data from Tx Mcu to Rx one. For checking purposes I put Printf function and delay into RDA interrupt. Arrived data is ok and printf(lcd_putc…. Prints. If commit out print function and move it to while routine, it is gone. What is wrong? What am I doing wrong?
I am using well known simulator program. Have not tried on silicon and my compiler version PCH 5.025. Thanks in advance.
Here is the snippet:
Code: |
#include <16f876A.h>
#include <stdlib.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay (clock=4000000)
#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, stop=1)
#include "input.c"
#define LCD_ENABLE_PIN PIN_A0
#define LCD_RS_PIN PIN_A1
#define LCD_RW_PIN PIN_A2
#define LCD_DATA4 PIN_C4
#define LCD_DATA5 PIN_C3
#define LCD_DATA6 PIN_C2
#define LCD_DATA7 PIN_C1
#include <LCD.C
int i,hiz=100;
char islem=0;
long Adim=0,Toplam=0;
const int yarim_adim[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09}; // Step motor //const int //tam_adim [4] = {0x9,0x0c,0x06, 0x03}; //{0x05,0X09,0X10,0X06}
Long Data,Ldata;
signed int16 RX_data;
#int_rda
void serihaberlesme_kesmesi ()
{
RX_data=get_int16();
printf(lcd_putc,"\fi DATA:%lu"RX_data,);Delay_ms(500);
/*it is for checking purposes es No Print no Delay*/
}
void main ()
{
setup_timer_1(T1_DISABLED);
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(0b11000000);
output_b(0x00);
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
lcd_init();
if(input(pin_B6)){Toplam= 0;write_long_to_eeprom(10,toplam);Delay_ms(30); }
if(!input(pin_B6)){Toplam=read_long_from_eeprom(10);Delay_ms(30); }
while(1)
{
// printf(lcd_putc,"\fRXData:%lu"RX_data,);Delay_ms(500);
// clear_interrupt(int_rda);
} |
+++++++++++++++
Added code block.
- Forum Moderator
+++++++++++++++ |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
|
|
Posted: Thu Mar 31, 2016 2:30 am |
|
|
First some comments:
1) Use the code buttons!.....
2) When debugging, part of the skill is to trim down to just the minimum showing the problem. At least 50% of the values and code there, is not to do with the problem. Get rid of it.
3) Repeat 50*. #USE RS232, _must_ always have 'ERRORS' used, except when you are handling these yourself. This is one that (unfortunately) CCS's own demos sometimes omit, but _must_ be there. Otherwise your UART _will_ be hung.
4) Then understand the next thing. You should not be using input.c functions inside the interrupt. The interrupt says _one_ character has been received. Just one. get_int16 wants a whole sequence of characters. Input.c, is a set of tools and examples. If you want an equivalent to get_int16, for use inside the interrupt, you have to write it. The interrupt should (must) only read one character and exit. Nothing else. The example for using INT_RDA, is ex_sisr.c. However a routine somewhat like Get_int16 can be written to be part of the interrupt.
However to do a 'full' version with backspace etc., will be complex. I've kept it simple.
Lots of comments in the code.
Code: |
#include <16f876A.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,PUT,NOWRT,NODEBUG,NOCPD
//When using a crystal, you should have PUT
#use delay (clock=4000000)
#use rs232 (baud=9600, UART1, parity=N, stop=1, ERRORS)
//shorthand to use the first UART _ERRORS_.....
#include <stdlib.h> //Only include things _after_ the full processor setup
//About 1 in 100 routines will not work correctly, till the clock etc.
//is setup. So get in the habit of doing this stuff _first_.
#define LCD_ENABLE_PIN PIN_A0
#define LCD_RS_PIN PIN_A1
#define LCD_RW_PIN PIN_A2
#define LCD_DATA4 PIN_C4
#define LCD_DATA5 PIN_C3
#define LCD_DATA6 PIN_C2
#define LCD_DATA7 PIN_C1
#include <LCD.C> //your closing bracket was missing
#define BUFSIZ 16
char temp[BUFSIZ]; //temporary serial buffer - allowing 15 characters
int1 RX_Has_data=FALSE; //flag for line-feed seen.
#int_rda
void serihaberlesme_kesmesi (void)
{
static int8 ctr=0;
int8 chr;
chr=getc(); //Just one character available
if (chr==13)
{
//If we have an end of line
temp[ctr]='\0'; //null terminate string
RX_has_data=TRUE; //flag that there is a string
ctr=0; //clear the counter
}
else
{
temp[ctr++]=chr; //add character to string
if (ctr>=BUFSIZ-1)
ctr--; //ensure buffer can not overflow
}
}
void main (void)
{
unsigned int16 RX_data;
setup_timer_1(T1_DISABLED);
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(0b11000000); //Pointless - the next instruction sets TRIS
output_b(0x00);
//Generally you should pause for a moment to allow the RS232 transceivers
//to correctly wake up, and a lot of LCD's _will_ require some delay before
//they are initialised. Hence delaying
delay_ms(500);
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
lcd_init();
while(1)
{
//Now there is only a string to process, if RX_has_data is TRUE
if (RX_has_data)
{
RX_data=atol(temp); //read the number from the buffer
RX_has_data=FALSE;
printf(lcd_putc,"\fi DATA:%lu",RX_data);
//You had RX_data declares as 'signed' but are printing it as unsigned
//I have switched to using unsigned.
}
}
}
|
If you want to use the get_int16 function, then you need to switch to not using interrupt driven serial. With interrupts, the code in the interrupt, must handle the hardware event (one character available), and get out. You can't use things like input.c functions in the interrupt.
As posted, this code will not echo the characters back. If you want this, just putc the character after it is received. |
|
|
Chevy64
Joined: 05 Apr 2013 Posts: 7
|
TNX |
Posted: Thu Mar 31, 2016 3:44 am |
|
|
My special thanks goes to you guys. I appreciated and learned much.. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Thu Mar 31, 2016 5:23 am |
|
|
re: delay_ms(500); before lcd_init();
As Mr T points out LCD modules need some time to 'get organized'. There is NO standard time and I had on batch that needed almost 600ms!
If 500ms works fine, great if the LCDm doesn't respond,just increase the delay value. More delay is better than 'just enough'.
re:
I am using well known simulator program
You should be aware that NONE of the PIC projects presented as a 'schematic' here using a using well known simulator program can ACTUALLY work using silicon in the Real World ! At least 100% of all the Proteus simulations won't.
Jay |
|
|
|
|
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
|