free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems
Hi everybody! I've just joined Rickey's World. I'm Joaquim, I hope I'll be around here during this year. I've worked in the RFID area so I'll try to help if there is somebody with a doubt in that area I've search for a solution to my problem for a couple of days in this and others forums and there are people in the same situation, however, I've not been able to apply their solutions to my problem. I hope somebody in this forum will In fact, I don't know what I'm doing wrong! Here is the code:
CODE:
#include "reg_c51.h" #include <stdio.h>
char uart_data; char caracter; short index; char trama[20];
char getchar (void) { char chr;/* variable to hold the new character */ while(RI !=1){;} /* now read the value in the serial buffer into the local variable */
chr = SBUF;
RI =0; /* the character is then returned to the calling function. */ return(chr); }
char putchar(char chr){
SBUF = chr; }
/**
* FUNCTION_PURPOSE: This file set up uart in mode 1 (8 bits uart) with
* timer 2 in baud rate generator mode.
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: void
*/ void main (void) {
SCON =0x50;/* uart in mode 1 (8 bit), REN=1 */
T2CON &=0xF0;/* EXEN2=0; TR2=0; C/T2#=0; CP/RL2#=0; */
T2CON |=0x30;/* RCLK = 1; TCLK=1; */
TH2=0xFF;/* init value */
TL2=0xFD;/* init value */
RCAP2H=0xFF;/* reload value, 115200 Bds at 11.059MHz */
RCAP2L=0xFD;/* reload value, 115200 Bds at 11.059MHz */
ES =1;/* Enable serial interrupt */
EA =1;/* Enable global interrupt */
TR2 =1;/* Timer 2 run */
TI =1;
/**
* FUNCTION_PURPOSE: serial interrupt, echo received data.
* FUNCTION_INPUTS: P3.0(RXD) serial input
* FUNCTION_OUTPUTS: P3.1(TXD) serial output
*/ void serial_IT(void)interrupt4 { if(RI ==1) {/* if reception occur */
RI =0;/* clear reception flag for next reception */
uart_data = SBUF;/* Read receive data */ //SBUF = uart_data; /* Send back same data on uart*/ } else TI =0;/* if emission occur */ /* clear emission flag for next emission*/ }
Simple, isn't it? I think this code is so known by any programmer. My idea is to collect some characters and print the entire string when an "enter" is pressed. When this is achieved, if there is some constant string like "hello8051guys" no problem arise. When I comment that one and try to get the char array I've been building, there is no answer! I have redefined putchar and getchar but anything appears on the screen Does anybody have a great idea? Perhaps am I doing something incredibly wrong? Well, I'll be waiting for your wise answers.
Sorry for my English, I'm from Catalonia! Have a good night!
No luck! In fact I rely on the initial "0x00" positions on the char array to detect the string end. I attach an image with the situation I obtain in KEIL.
In the "Watch" window we can see the string is well build and in the "Serial #1" window we can see the string is on the 3rd line... it really detects the "enter" but no string is displayed.
Another suggestion? Perhaps I'm misunderstanding something?
try initialising index to 0 before the wile loop lProgress is not made by early risers or hard workers, but by LAZY people, trying to find easier ways to do the same........
Hehe, thanks for helping Shyam! No luck again Anybody thinks the threat in the following link http://www.keil.com/forum/docs/thread6798.asp is useful? I'm working with a AT89C51ED2, the clock rate = 22.1184 I know the code is for a 11.0592MHz cristal, but it would not explain why a constant string like "hello8051guys" is well processed, would it?
printf and getchar functions doest work with interrupts.. disable interrupts and try again www.rickeyworld.info If you feel satisfied with the user's forum reply please click on the thank button.
Thanks to everybody! Dave, cristal is 22.1184. I have been thinking about the problem during all this afternoon. It seems it is a very common problem and the solution is found at http://www.keil.com/forum/docs/thread11351.asp
// -------------------------------------------------------------------- // -------------------------------------------------------------------- void main (void) {
char Junk;
EA =0;/* disable interrupts... */
SCON0 =0x50;/* Setup serial port registers */
TI =0;/* clear transmit interrupt */
RI =0;/* clear receiver interrupt */
ES0 =1;/* enable serial interrupts */
PS0 =0;/* set serial interrupts to low priority */
EA =1;/* Enable Interrupts */
ES0 =0;/* disable interrupts */
TI =0;/* Clear interrupt flag and char buffers */
TxInUse =0;/* transmitter is disabled */
RxWaiting =1;/* receiver is waiting for a character... */
TR1 =0;/* stop timer 1 */
ET1 =0;/* disable timer 1 interrupt */
PCON |=0x80;/* 0x40=SMOD1: set serial baudrate doubler */
TMOD &=0x0F;/* clear timer 1 mode bits */
TMOD |=0x20;/* timer 1 into MODE 2 : 8-bit, auto-reload */
TH1 =(unsignedchar)(256.0-((float)(FREQ)/(192.0*(float)(BAUDRATE))));
TR1 =1;/* start timer 1 */
ES0 =1;/* enable interrupts */
/* some simple text... */ printf("This is a sample string\r\n"); printf("Nothing very important\r\n"); for( Junk='0'; Junk<='9'; Junk++)
putchar( Junk ); printf("\r\n\n");
/* a simple character echo routine */ printf("Type all you want...\r\n"); while(1) {
putchar( getchar()); } }
// -------------------------------------------------------------------- // -------------------------------------------------------------------- void SER_ISR()interrupt UART0_VECTOR { /* Received value interrupt */ if( RxWaiting ) { if( RI ) {
RI =0;
/* get byte if there's room in the buffer */
RxInChar = SBUF0;
RxWaiting =0; } }
/* Transmitted value interrupt */ if( TxInUse ) { /* if transmit flag is set, then previous byte is finished transmitting */ if( TI ) {
TI =0;
TxInUse =0; } } }
-Dave "Basic research is what I am doing when I don't know what I am doing"
8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems