Working uart_init function

This commit is contained in:
7u83 2024-05-25 19:15:01 +02:00
parent a2a9256061
commit 151891240d
4 changed files with 46 additions and 54 deletions

View File

@ -1,7 +1,7 @@
SDCC ?= sdcc SDCC ?= sdcc
SDAR ?= sdar SDAR ?= sdar
STCCODESIZE ?= 8192 STCCODESIZE ?= 8192
SDCCOPTS ?= -mmcs51 --iram-size 512 --code-size $(STCCODESIZE) --xram-size 0 --opt-code-size --stack-auto SDCCOPTS ?= -mmcs51
SDCCREV ?= -Dstc15f204ea SDCCREV ?= -Dstc15f204ea
STCGAL ?= stcgal STCGAL ?= stcgal
#/stcgal.py #/stcgal.py
@ -13,7 +13,7 @@ FLASHFILE ?= main.ihx
#FOSC ?=11056000L #FOSC ?=11056000L
CFLAGS ?= -DWITH_ALT_LED9 -DWITHOUT_LEDTABLE_RELOC -DSHOW_TEMP_DATE_WEEKDAY CFLAGS ?= -DWITH_ALT_LED9 -DWITHOUT_LEDTABLE_RELOC -DSHOW_TEMP_DATE_WEEKDAY
LIBSRC = uart_init.c uart_send_chr.c uart_send_str.c \ LIBSRC = uart_init_.c uart_send_chr.c uart_send_str.c \
rotary_encoder_stat.c \ rotary_encoder_stat.c \
sevenseg_dec.c sevenseg_dec_inv.c \ sevenseg_dec.c sevenseg_dec_inv.c \
int_to_bcd.c getbutton.c int_to_bcd.c getbutton.c

View File

@ -6,12 +6,19 @@
void uart_init(long fosc, long baud);
void uart_send_chr(char tx_data); void uart_send_chr(char tx_data);
void uart_send_str(const char *str); void uart_send_str(const char *str);
void uart_init_(uint8_t timer_init,uint8_t x2);
#define uart_init(baud,x2) \
uart_init_(256L-FOSC/12L/(baud*16L/(1<<x2)),x2)
#define setbits(var,bits) (var|=bits)
#define unsetbits(var,bits) (var&=(bits^0xff))
int8_t rotary_encoder_stat(uint8_t b); int8_t rotary_encoder_stat(uint8_t b);
@ -46,5 +53,16 @@ int getbutton(uint8_t pinval, uint8_t *button);
extern uint8_t getbutton_time; extern uint8_t getbutton_time;
/* a different representation of pins, which
* lets us iteratate over pins.
* But its slower, of course */
typedef struct {
uint8_t port;
uint8_t pin;
} i8051pin_T;
#endif #endif

View File

@ -1,49 +0,0 @@
#include "mc8051fun.h"
//#include <stc12.h>
#include <at89c51ed2.h>
void uart_init(long fosc, long baud)
{
TMOD |= 0x20; // Timer 1 in Mode 2
TH1 = 0xFD; // 9600 baud rate
TL1 = 0xFD; // Initial value (not necessary)
TR1 = 1; // Start Timer 1
SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled
return;
TMOD = 0; /* Timer 1, 8-bit auto reload mode */
// P_SW1 |= (1 << 6); // move UART1 pins -> P3_6:rxd, P3_7:txd
// UART1 use Timer2
// T2L = (65536 - (fosc / 4 / baud)) & 0xFF;
// T2H = (65536 - (fosc / 4 / baud)) >> 8;
SM0 = 0;
SM1 = 1; // serial mode 1: 8-bit async
// AUXR |= 0x14; // T2R: run T2, T2x12: T2 clk src sysclk/1
// AUXR |= 0x01; // S1ST2: T2 is baudrate generator
SCON = 0x50;
TMOD |= T1_M1;
TI=1;
TCLK=0;
RCLK=0;
// TBCK=0;
// RBCK=0;
//
TH1=0xFD;
//SMOD0=0;
ES = 1; // enable uart1 interrupt
EA = 1; // enable interrupts
REN = 1;
}

23
uart_init_.c Normal file
View File

@ -0,0 +1,23 @@
#include "mc8051fun.h"
#include <at89c51ed2.h>
void uart_init_(uint8_t timer_init,uint8_t x2)
{
TMOD |= 0x20; // Timer 1 in Mode 2
TH1 = timer_init;
TL1 = timer_init;
TR1 = 1; // Start Timer 1
SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled
if(x2)
setbits(PCON,SMOD);
else
unsetbits(PCON,SMOD);
return;
}