APP 1 S5.

Dependencies:   mbed

Committer:
IC_Blood
Date:
Sun Jan 10 18:21:11 2016 +0000
Revision:
5:c6f70b61aea5
Child:
6:ccdbd5923e37
First register test.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IC_Blood 5:c6f70b61aea5 1 #include <UART.h>
IC_Blood 5:c6f70b61aea5 2 #include <stdio.h>
IC_Blood 5:c6f70b61aea5 3
IC_Blood 5:c6f70b61aea5 4 //#define BAUD_RATE 9600
IC_Blood 5:c6f70b61aea5 5 //#define IN_BUF_SIZE 256
IC_Blood 5:c6f70b61aea5 6
IC_Blood 5:c6f70b61aea5 7 //#define TRUE 1
IC_Blood 5:c6f70b61aea5 8 //#define FALSE 0
IC_Blood 5:c6f70b61aea5 9
IC_Blood 5:c6f70b61aea5 10 //static unsigned char inbuf[IN_BUF_SIZE];
IC_Blood 5:c6f70b61aea5 11 //static unsigned char qin = 0;
IC_Blood 5:c6f70b61aea5 12 //static unsigned char qout = 0;
IC_Blood 5:c6f70b61aea5 13
IC_Blood 5:c6f70b61aea5 14 //static char flag_rx_waiting_for_stop_bit;
IC_Blood 5:c6f70b61aea5 15 //static char flag_rx_off;
IC_Blood 5:c6f70b61aea5 16 //static char rx_mask;
IC_Blood 5:c6f70b61aea5 17 //static char flag_rx_ready;
IC_Blood 5:c6f70b61aea5 18 //static char flag_tx_ready;
IC_Blood 5:c6f70b61aea5 19 //static char timer_rx_ctr;
IC_Blood 5:c6f70b61aea5 20 //static char timer_tx_ctr;
IC_Blood 5:c6f70b61aea5 21 //static char bits_left_in_rx;
IC_Blood 5:c6f70b61aea5 22 //static char bits_left_in_tx;
IC_Blood 5:c6f70b61aea5 23 //static char rx_num_of_bits;
IC_Blood 5:c6f70b61aea5 24 //static char tx_num_of_bits;
IC_Blood 5:c6f70b61aea5 25 //static int internal_rx_buffer;
IC_Blood 5:c6f70b61aea5 26 //static int internal_tx_buffer;
IC_Blood 5:c6f70b61aea5 27 //static int user_tx_buffer;
IC_Blood 5:c6f70b61aea5 28
IC_Blood 5:c6f70b61aea5 29 UART::UART (int baud_rate)
IC_Blood 5:c6f70b61aea5 30 {
IC_Blood 5:c6f70b61aea5 31 baudRate = 9600;
IC_Blood 5:c6f70b61aea5 32 }
IC_Blood 5:c6f70b61aea5 33
IC_Blood 5:c6f70b61aea5 34 //void UART::initUart()
IC_Blood 5:c6f70b61aea5 35 //{
IC_Blood 5:c6f70b61aea5 36 // flag_tx_ready = FALSE;
IC_Blood 5:c6f70b61aea5 37 // flag_rx_ready = FALSE;
IC_Blood 5:c6f70b61aea5 38 // flag_rx_waiting_for_stop_bit = FALSE;
IC_Blood 5:c6f70b61aea5 39 // flag_rx_off = FALSE;
IC_Blood 5:c6f70b61aea5 40 // rx_num_of_bits = 10;
IC_Blood 5:c6f70b61aea5 41 // tx_num_of_bits = 10;
IC_Blood 5:c6f70b61aea5 42
IC_Blood 5:c6f70b61aea5 43 // set_tx_pin_low();
IC_Blood 5:c6f70b61aea5 44 //ticker.attach_us(&timer_isr, 1000000.0 / (BAUD_RATE * 3.0));
IC_Blood 5:c6f70b61aea5 45 //}
IC_Blood 5:c6f70b61aea5 46
IC_Blood 5:c6f70b61aea5 47
IC_Blood 5:c6f70b61aea5 48 //DigitalOut TX(p25);
IC_Blood 5:c6f70b61aea5 49 //DigitalIn RX(p26);
IC_Blood 5:c6f70b61aea5 50 //Ticker ticker;
IC_Blood 5:c6f70b61aea5 51
IC_Blood 5:c6f70b61aea5 52 //Serial pc(USBTX, USBRX);
IC_Blood 5:c6f70b61aea5 53
IC_Blood 5:c6f70b61aea5 54
IC_Blood 5:c6f70b61aea5 55 //Background functions to execute while waiting for input.
IC_Blood 5:c6f70b61aea5 56 //void idle(){
IC_Blood 5:c6f70b61aea5 57 // wait_us(1.0);
IC_Blood 5:c6f70b61aea5 58 //}
IC_Blood 5:c6f70b61aea5 59
IC_Blood 5:c6f70b61aea5 60 //Sets the transmit pin to the high state.
IC_Blood 5:c6f70b61aea5 61 //void set_tx_pin_high() {
IC_Blood 5:c6f70b61aea5 62 // TX = 1;
IC_Blood 5:c6f70b61aea5 63 //}
IC_Blood 5:c6f70b61aea5 64
IC_Blood 5:c6f70b61aea5 65 //Sets the transmit pin to the low state.
IC_Blood 5:c6f70b61aea5 66 //void UART::set_tx_pin_low()
IC_Blood 5:c6f70b61aea5 67 //{
IC_Blood 5:c6f70b61aea5 68 // TX = 0;
IC_Blood 5:c6f70b61aea5 69 //}
IC_Blood 5:c6f70b61aea5 70
IC_Blood 5:c6f70b61aea5 71 //Returns 0 or 1 dependent on whether the receive pin is high or low
IC_Blood 5:c6f70b61aea5 72 //int get_rx_pin_status() {
IC_Blood 5:c6f70b61aea5 73 // return RX.read();
IC_Blood 5:c6f70b61aea5 74 //}
IC_Blood 5:c6f70b61aea5 75
IC_Blood 5:c6f70b61aea5 76 //void timer_isr(void)
IC_Blood 5:c6f70b61aea5 77 // {
IC_Blood 5:c6f70b61aea5 78 // char mask, start_bit, flag_in;
IC_Blood 5:c6f70b61aea5 79 //
IC_Blood 5:c6f70b61aea5 80
IC_Blood 5:c6f70b61aea5 81 // Transmitter Section
IC_Blood 5:c6f70b61aea5 82 // if ( flag_tx_ready )
IC_Blood 5:c6f70b61aea5 83 // {
IC_Blood 5:c6f70b61aea5 84 //
IC_Blood 5:c6f70b61aea5 85 // if ( --timer_tx_ctr<=0 )
IC_Blood 5:c6f70b61aea5 86 // {
IC_Blood 5:c6f70b61aea5 87 // mask = internal_tx_buffer&1;
IC_Blood 5:c6f70b61aea5 88 // internal_tx_buffer >>= 1;
IC_Blood 5:c6f70b61aea5 89 // if ( mask )
IC_Blood 5:c6f70b61aea5 90 // {
IC_Blood 5:c6f70b61aea5 91 // set_tx_pin_high();
IC_Blood 5:c6f70b61aea5 92 // }
IC_Blood 5:c6f70b61aea5 93 // else
IC_Blood 5:c6f70b61aea5 94 // {
IC_Blood 5:c6f70b61aea5 95 // set_tx_pin_low();
IC_Blood 5:c6f70b61aea5 96 // }
IC_Blood 5:c6f70b61aea5 97 // timer_tx_ctr = 3;
IC_Blood 5:c6f70b61aea5 98 // if ( --bits_left_in_tx<=0 )
IC_Blood 5:c6f70b61aea5 99 // {
IC_Blood 5:c6f70b61aea5 100 // flag_tx_ready = FALSE;
IC_Blood 5:c6f70b61aea5 101 // }
IC_Blood 5:c6f70b61aea5 102 // }
IC_Blood 5:c6f70b61aea5 103 // }
IC_Blood 5:c6f70b61aea5 104 // Receiver Section
IC_Blood 5:c6f70b61aea5 105 // if ( flag_rx_off==FALSE )
IC_Blood 5:c6f70b61aea5 106 // {
IC_Blood 5:c6f70b61aea5 107 // if ( flag_rx_waiting_for_stop_bit )
IC_Blood 5:c6f70b61aea5 108 // {
IC_Blood 5:c6f70b61aea5 109 // if ( --timer_rx_ctr<=0 )
IC_Blood 5:c6f70b61aea5 110 // {
IC_Blood 5:c6f70b61aea5 111 // flag_rx_waiting_for_stop_bit = FALSE;
IC_Blood 5:c6f70b61aea5 112 // flag_rx_ready = FALSE;
IC_Blood 5:c6f70b61aea5 113 // internal_rx_buffer &= 0xFF;
IC_Blood 5:c6f70b61aea5 114 // if ( internal_rx_buffer!=0xC2 )
IC_Blood 5:c6f70b61aea5 115 // {
IC_Blood 5:c6f70b61aea5 116 // inbuf[qin] = internal_rx_buffer;
IC_Blood 5:c6f70b61aea5 117 // if ( ++qin>=IN_BUF_SIZE )
IC_Blood 5:c6f70b61aea5 118 // {
IC_Blood 5:c6f70b61aea5 119 // qin = 0;
IC_Blood 5:c6f70b61aea5 120 // }
IC_Blood 5:c6f70b61aea5 121 // }
IC_Blood 5:c6f70b61aea5 122 // }
IC_Blood 5:c6f70b61aea5 123 // }
IC_Blood 5:c6f70b61aea5 124 // else // rx_test_busy
IC_Blood 5:c6f70b61aea5 125 // {
IC_Blood 5:c6f70b61aea5 126 // if ( flag_rx_ready==FALSE )
IC_Blood 5:c6f70b61aea5 127 // {
IC_Blood 5:c6f70b61aea5 128 // start_bit = get_rx_pin_status();
IC_Blood 5:c6f70b61aea5 129 // Test for Start Bit
IC_Blood 5:c6f70b61aea5 130 // if ( start_bit==0 )
IC_Blood 5:c6f70b61aea5 131 // {
IC_Blood 5:c6f70b61aea5 132 // flag_rx_ready = TRUE;
IC_Blood 5:c6f70b61aea5 133 // internal_rx_buffer = 0;
IC_Blood 5:c6f70b61aea5 134 // timer_rx_ctr = 4;
IC_Blood 5:c6f70b61aea5 135 // bits_left_in_rx = rx_num_of_bits;
IC_Blood 5:c6f70b61aea5 136 // rx_mask = 1;
IC_Blood 5:c6f70b61aea5 137 // }
IC_Blood 5:c6f70b61aea5 138 // }
IC_Blood 5:c6f70b61aea5 139 // else // rx_busy
IC_Blood 5:c6f70b61aea5 140 // {
IC_Blood 5:c6f70b61aea5 141 // if ( --timer_rx_ctr<=0 )
IC_Blood 5:c6f70b61aea5 142 // { // rcv
IC_Blood 5:c6f70b61aea5 143 // timer_rx_ctr = 3;
IC_Blood 5:c6f70b61aea5 144 // flag_in = get_rx_pin_status();
IC_Blood 5:c6f70b61aea5 145 // if ( flag_in )
IC_Blood 5:c6f70b61aea5 146 // {
IC_Blood 5:c6f70b61aea5 147 // internal_rx_buffer |= rx_mask;
IC_Blood 5:c6f70b61aea5 148 // }
IC_Blood 5:c6f70b61aea5 149 // rx_mask <<= 1;
IC_Blood 5:c6f70b61aea5 150 // if ( --bits_left_in_rx<=0 )
IC_Blood 5:c6f70b61aea5 151 // {
IC_Blood 5:c6f70b61aea5 152 // flag_rx_waiting_for_stop_bit = TRUE;
IC_Blood 5:c6f70b61aea5 153 // }
IC_Blood 5:c6f70b61aea5 154 // }
IC_Blood 5:c6f70b61aea5 155 // }
IC_Blood 5:c6f70b61aea5 156 // }
IC_Blood 5:c6f70b61aea5 157 // }
IC_Blood 5:c6f70b61aea5 158 // }
IC_Blood 5:c6f70b61aea5 159
IC_Blood 5:c6f70b61aea5 160 //char _getchar( void )
IC_Blood 5:c6f70b61aea5 161 // {
IC_Blood 5:c6f70b61aea5 162 // char ch;
IC_Blood 5:c6f70b61aea5 163
IC_Blood 5:c6f70b61aea5 164 // do
IC_Blood 5:c6f70b61aea5 165 // {
IC_Blood 5:c6f70b61aea5 166 // while ( qout==qin )
IC_Blood 5:c6f70b61aea5 167 // {
IC_Blood 5:c6f70b61aea5 168 // idle();
IC_Blood 5:c6f70b61aea5 169 // }
IC_Blood 5:c6f70b61aea5 170 // ch = inbuf[qout] & 0xFF;
IC_Blood 5:c6f70b61aea5 171 // if ( ++qout>=IN_BUF_SIZE )
IC_Blood 5:c6f70b61aea5 172 // {
IC_Blood 5:c6f70b61aea5 173 // qout = 0;
IC_Blood 5:c6f70b61aea5 174 // }
IC_Blood 5:c6f70b61aea5 175 // }
IC_Blood 5:c6f70b61aea5 176 // while ( ch==0x0A || ch==0xC2 );
IC_Blood 5:c6f70b61aea5 177 // return( ch );
IC_Blood 5:c6f70b61aea5 178 // }
IC_Blood 5:c6f70b61aea5 179
IC_Blood 5:c6f70b61aea5 180 //void _putchar( int ch )
IC_Blood 5:c6f70b61aea5 181 // {
IC_Blood 5:c6f70b61aea5 182 // while ( flag_tx_ready );
IC_Blood 5:c6f70b61aea5 183 // user_tx_buffer = ch;
IC_Blood 5:c6f70b61aea5 184
IC_Blood 5:c6f70b61aea5 185 // invoke_UART_transmit
IC_Blood 5:c6f70b61aea5 186 // timer_tx_ctr = 3;
IC_Blood 5:c6f70b61aea5 187 // bits_left_in_tx = tx_num_of_bits;
IC_Blood 5:c6f70b61aea5 188 // internal_tx_buffer = (user_tx_buffer<<1) | 0x200;;
IC_Blood 5:c6f70b61aea5 189 // flag_tx_ready = TRUE;
IC_Blood 5:c6f70b61aea5 190 // }
IC_Blood 5:c6f70b61aea5 191
IC_Blood 5:c6f70b61aea5 192 //void flush_input_buffer( void )
IC_Blood 5:c6f70b61aea5 193 // {
IC_Blood 5:c6f70b61aea5 194 // qin = 0;
IC_Blood 5:c6f70b61aea5 195 // qout = 0;
IC_Blood 5:c6f70b61aea5 196 // }
IC_Blood 5:c6f70b61aea5 197
IC_Blood 5:c6f70b61aea5 198 //char kbhit( void )
IC_Blood 5:c6f70b61aea5 199 // {
IC_Blood 5:c6f70b61aea5 200 // return( qin!=qout );
IC_Blood 5:c6f70b61aea5 201 // }
IC_Blood 5:c6f70b61aea5 202
IC_Blood 5:c6f70b61aea5 203 //void turn_rx_on( void )
IC_Blood 5:c6f70b61aea5 204 // {
IC_Blood 5:c6f70b61aea5 205 // flag_rx_off = FALSE;
IC_Blood 5:c6f70b61aea5 206 // }
IC_Blood 5:c6f70b61aea5 207
IC_Blood 5:c6f70b61aea5 208 //void turn_rx_off( void )
IC_Blood 5:c6f70b61aea5 209 // {
IC_Blood 5:c6f70b61aea5 210 // flag_rx_off = TRUE;
IC_Blood 5:c6f70b61aea5 211 // }
IC_Blood 5:c6f70b61aea5 212
IC_Blood 5:c6f70b61aea5 213 //void printStr(char* str)
IC_Blood 5:c6f70b61aea5 214 //{
IC_Blood 5:c6f70b61aea5 215 // int i = 0;
IC_Blood 5:c6f70b61aea5 216 // int len = strlen(str);
IC_Blood 5:c6f70b61aea5 217 // for(i = 0; i<len; i++)
IC_Blood 5:c6f70b61aea5 218 // {
IC_Blood 5:c6f70b61aea5 219 // wait(0.01);
IC_Blood 5:c6f70b61aea5 220 // _putchar(str[i]);
IC_Blood 5:c6f70b61aea5 221 // }
IC_Blood 5:c6f70b61aea5 222 //}