I am developing a DMX Rx module. I thought it would be good to have a TX module that gave a known refernece signal ( it also enabled me to check the baud rate)
Thus the following code was born.
While the code may not be too glamerous, it seems to works.
If it is useful to others, that's good.
All comments on the code are welcome
Matt
(I am using a TINY231 and GCC with in Studio4)
/* io.h contains the defines for USART registers */ #include#include "delay_x.h" /* Defines to calculate the UBBR value based on 12Mhz clock speed ** and 250 000 baud. */ #define F_CPU 12000000L #define FOSC 12000000L #define BAUD 250000 #define MYUBRR FOSC/16/BAUD-1 #define dmx_ch 192 #define LED_PORT PORTD #define LED_IO PD6 // indicates program initialisation status #define LED_USART PD5 // Indicates USART initialieds #define LED_TRIG PD4 // Really for testing purposes only. toogles with start frame #define LED_DATA PD3 // #define IO_TRIG PD2 // Really for testing purposes only. toogles with start frame #define IO_TX PD1 // Tx Pin of USART void USART_init(unsigned int ubrr); void USART_tx(unsigned char data); int main() { unsigned char count; unsigned char x; DDRD = 0b01111001; LED_PORT &= 0x00; LED_PORT |= (1<<LED_IO); // set bit 7 USART_init( MYUBRR ); LED_PORT |= (1<<LED_USART); // set bit 6 while(1) { LED_PORT &=~(1<<IO_TX); // 'start' frame is low for 88 to 100 us LED_PORT |=(1<<IO_TRIG); // Set trigger high for testing only LED_PORT |=(1<<LED_TRIG); // turn Trigger LED on _delay_us(94); // this delay is twicked LED_PORT |=(1<<IO_TX); // MAB is high for atleast 8 us LED_PORT &=~(1<<IO_TRIG); // Set the trigger pulse low _delay_us(10); UCSRA = (1<<TXC); // Clear out any UCSRB = (1<<TXEN); // Enable tx portion of the USART LED_PORT &=~(1<<LED_TRIG); // Turn Trigger LED off x = 0x00; for (count=1; count <= dmx_ch; count++) { // the first word has all 0s indication start co USART_tx(x); x=x+0x01; } while (!(UCSRA&(1<<TXC))); // wait here until the last data bit is TXd UCSRB &=~(1<<TXEN); // disable the TX portion of the Usart } return(0); } /*********** USART_tx ******************************************/ void USART_tx(unsigned char data) { while( !( UCSRA & (1<<UDRE)) ) ; UDR = data; } /*********** USART_init ******************************************/ void USART_init(unsigned int ubrr) { /* set baud rate, UBRRH is not used for this example */ UBRRH = (unsigned char)(ubrr>>8); UBRRL = (unsigned char)ubrr; /* Don't enable rx and tx YET*/ // UCSRB = (1<<RXEN) | (1<<TXEN); UCSRB &= ~(1 << RXEN) ; UCSRB &= ~(1 << TXEN) ; /* set frame settings to 8 data, 2 stop bit */ UCSRC = (1<<USBS) | (3<<UCSZ0); }