??? 12/28/04 20:06 Read: times |
#83993 - Timer 2 baud generation problem |
Hi all...
Let me preface by saying I've searched the archives, read the bible, the tutorial, and FAQ, and I still don't see why I'm having this problem... It goes like this: I wrote some very simple serial routines that were working just fine for me if I used Timer 1 as the baud rate generator. I'm now trying to make it work using Timer 2 as the baud rate generator, and I'm getting nothing. I'm running an Atmel AT89C52 chip at 11.059MHz. I'm trying to generate a 2400 baud rate for both transmit and receive. My code follows. If at the beginning of my program, I call subroutine serial_init_t1, it works fine, but I'm trying to make it work with serial_init_t2. (serial_init_t1 is included for reference) Any help is much appreciated. It's driving me crazy. -Sam ;8052 Serial port HELLO WORLD program ;Trying to run on a ATMEL AT89C52 at 11.059MHz wired to a MAX232CPE chip ;output to PC in HyperTerminal at 2400 baud 8/N/1 with no flow control, echo off .org 0x0000 ;***********MAIN PROGRAM LOOP****************** acall serial_init_t2 ;Initialize the serial port - use timer 2 mov dptr, #msg_hello ;point the data pointer to the start of the Hello World message acall send_msg_at_dptr ;send the message to the serial port endless_loop: mov a, #'>' ;display the prompt acall serial_out acall serial_in ;wait for a character acall serial_out ;echo acall serial_cr ;CR/LF sjmp endless_loop ;loop ;***********SERIAL ROUTINES***************** serial_init_t1: ;***Initialize serial port using Timer 1 for 2400 w/ 11.059MHz clock ;NOT CALLED in this program, but included as reference mov PCON, #00H ;clear SMOD - baud rate NOT doubled ; so K = 1 in baud equation below mov TMOD, #22H ;set Timers 1 and 0 to 8-bit auto-reload mov TH1, #0xF4H ;sets timer 1 for 2400 baud assuming 11.059MHz clock ; TH1 = 256 - ((K * OscFreq)/(384 * BaudRate)) ; = 256 - ((1 * 11059000)/(384 * 2400)) ; = 256 - 11.9997829861111 ; = 244 = #0xF4H setb TCON.6 ;same as setb TR1 - starts timer 1 running mov SCON, #50H ;Serial port mode 1 - 8bit with variable baud, receive enabled, clear Transmit interrupt flag ret serial_init_t2: ;***Initialize serial port - using Timer 2 for 2400 w/ 11.059MHz clock mov PCON, #00H ;clear SMOD - baud rate NOT doubled mov T2CON, #00110000b ;Set Timer 2 as auto-reload counter to control both Receive and Transmit serial clocks - not running yet. mov RCAP2H, #0xFFh ;set T2 reload values for 2400 baud as determined by http://www.keil.com/c51/baudrate.asp mov RCAP2L, #0x70h mov TH2, RCAP2H ;preload the timer high byte mov TL2, RCAP2L ;preload the timer low byte setb T2CON.2 ;start T2 running mov SCON, #0101000b ;Serial port mode 1 - 8bit with variable baud, receive enabled, clear Transmit interrupt flag ret send_msg_at_dptr: ;***Send 0-terminated string at dptr push acc ;save original values push dpl push dph clr a movc a, @a+dptr ;get first letter to display, put in ACC next_letter: acall serial_out ;send value in ACC inc dptr ;increment data pointer clr a ;clear ACC movc a, @a+dptr ;get next letter to display jz return_from_msg ;exit subroutine if we hit the end of the string sjmp next_letter ;otherwise send the next letter return_from_msg: pop dph ;restore original values pop dpl pop acc ret serial_in: jnb RI, serial_in ;Wait for the 8051 to set the RI flag clr RI ;clear received flag mov A,SBUF ;Read the character from the serial port ret serial_out: ;***Send ACC Byte to the serial port clr TI ;clear the "transmitted" bit mov SBUF, a ;send the letter to the serial port acall wait_serial_out ;wait for letter to finish sending ret wait_serial_out: ;***Wait for Serial to clock out jnb TI, wait_serial_out ;loop until TI is set (the "transmitted" bit) ret serial_cr: ;***Send a CR/LF to the serial port push acc ;save acc mov a, #0x0D ;put an ASCII CR in the ACC acall serial_out ;send it mov a, #0x0A ;put an ASCII LF in the ACC acall serial_out ;send it pop acc ;restore acc ret ;***********DATA FOLLOWS***************** msg_hello: .db 13,10,13,10,"HELLO WORLD!",13,10,13,10,0 ;the zero is an end-of-string marker |
Topic | Author | Date |
Timer 2 baud generation problem | 01/01/70 00:00 | |
avoid binary, it is difficult to read | 01/01/70 00:00 | |
That was it! | 01/01/70 00:00 | |
how I used to use binary | 01/01/70 00:00 | |
Another approach | 01/01/70 00:00 | |
binary and defines | 01/01/70 00:00 | |
yet another![]() | 01/01/70 00:00 |