han back / Mbed OS CLEO_UART_DOTMATRIX
Committer:
SMART_CLEO
Date:
Thu Sep 28 03:32:27 2017 +0000
Revision:
0:3f43ed494764
Child:
1:93504aad8a05
SMART_CLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SMART_CLEO 0:3f43ed494764 1 #include "mbed.h"
SMART_CLEO 0:3f43ed494764 2 #include "font_5x7.h"
SMART_CLEO 0:3f43ed494764 3
SMART_CLEO 0:3f43ed494764 4 struct UART_buf
SMART_CLEO 0:3f43ed494764 5 {
SMART_CLEO 0:3f43ed494764 6 uint8_t STA;
SMART_CLEO 0:3f43ed494764 7 uint8_t MODE;
SMART_CLEO 0:3f43ed494764 8 uint8_t CMD;
SMART_CLEO 0:3f43ed494764 9 uint8_t LEN;
SMART_CLEO 0:3f43ed494764 10 uint8_t DATA[32];
SMART_CLEO 0:3f43ed494764 11 uint8_t END;
SMART_CLEO 0:3f43ed494764 12 };
SMART_CLEO 0:3f43ed494764 13
SMART_CLEO 0:3f43ed494764 14 // I2C address
SMART_CLEO 0:3f43ed494764 15 int DoT_ADDR = 0x71<<1;
SMART_CLEO 0:3f43ed494764 16
SMART_CLEO 0:3f43ed494764 17 I2C Dotmatrix(I2C_SDA, I2C_SCL);
SMART_CLEO 0:3f43ed494764 18
SMART_CLEO 0:3f43ed494764 19 char Data_BUF[17] = {0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
SMART_CLEO 0:3f43ed494764 20
SMART_CLEO 0:3f43ed494764 21 Serial SerialUART(PA_2, PA_3);
SMART_CLEO 0:3f43ed494764 22
SMART_CLEO 0:3f43ed494764 23 UART_buf RX_BUF;
SMART_CLEO 0:3f43ed494764 24
SMART_CLEO 0:3f43ed494764 25 void SerialUARTRX_ISR(void);
SMART_CLEO 0:3f43ed494764 26 void data_write(char *data, int len);
SMART_CLEO 0:3f43ed494764 27 void Command_Write(uint8_t com);
SMART_CLEO 0:3f43ed494764 28 void Dotmatrix_init(void);
SMART_CLEO 0:3f43ed494764 29 void char_data_write(char data);
SMART_CLEO 0:3f43ed494764 30
SMART_CLEO 0:3f43ed494764 31 int main() {
SMART_CLEO 0:3f43ed494764 32
SMART_CLEO 0:3f43ed494764 33 SerialUART.baud(115200);
SMART_CLEO 0:3f43ed494764 34
SMART_CLEO 0:3f43ed494764 35 SerialUART.attach(&SerialUARTRX_ISR);
SMART_CLEO 0:3f43ed494764 36
SMART_CLEO 0:3f43ed494764 37 Dotmatrix_init();
SMART_CLEO 0:3f43ed494764 38
SMART_CLEO 0:3f43ed494764 39 while(1)
SMART_CLEO 0:3f43ed494764 40 {
SMART_CLEO 0:3f43ed494764 41 }
SMART_CLEO 0:3f43ed494764 42 }
SMART_CLEO 0:3f43ed494764 43
SMART_CLEO 0:3f43ed494764 44 void SerialUARTRX_ISR(void)
SMART_CLEO 0:3f43ed494764 45 {
SMART_CLEO 0:3f43ed494764 46 static uint8_t RX_count = 0, RX_Len = 32, RX_Status = 0;
SMART_CLEO 0:3f43ed494764 47 uint8_t rx_da = SerialUART.getc();
SMART_CLEO 0:3f43ed494764 48 switch(RX_Status)
SMART_CLEO 0:3f43ed494764 49 {
SMART_CLEO 0:3f43ed494764 50 case 0:
SMART_CLEO 0:3f43ed494764 51 if(rx_da == 0x76)
SMART_CLEO 0:3f43ed494764 52 {
SMART_CLEO 0:3f43ed494764 53 RX_BUF.STA = rx_da;
SMART_CLEO 0:3f43ed494764 54 RX_Status++;
SMART_CLEO 0:3f43ed494764 55 }
SMART_CLEO 0:3f43ed494764 56 break;
SMART_CLEO 0:3f43ed494764 57 case 1:
SMART_CLEO 0:3f43ed494764 58 RX_BUF.MODE = rx_da;
SMART_CLEO 0:3f43ed494764 59 RX_Status++;
SMART_CLEO 0:3f43ed494764 60 break;
SMART_CLEO 0:3f43ed494764 61 case 2:
SMART_CLEO 0:3f43ed494764 62 RX_BUF.CMD = rx_da;
SMART_CLEO 0:3f43ed494764 63 RX_Status++;
SMART_CLEO 0:3f43ed494764 64 break;
SMART_CLEO 0:3f43ed494764 65 case 3:
SMART_CLEO 0:3f43ed494764 66 RX_BUF.LEN = rx_da;
SMART_CLEO 0:3f43ed494764 67 RX_Len = RX_BUF.LEN;
SMART_CLEO 0:3f43ed494764 68 RX_Status++;
SMART_CLEO 0:3f43ed494764 69 if(RX_Len == 0)
SMART_CLEO 0:3f43ed494764 70 RX_Status++;
SMART_CLEO 0:3f43ed494764 71 break;
SMART_CLEO 0:3f43ed494764 72 case 4:
SMART_CLEO 0:3f43ed494764 73 RX_BUF.DATA[RX_count] = rx_da;
SMART_CLEO 0:3f43ed494764 74 RX_count++;
SMART_CLEO 0:3f43ed494764 75 if(RX_count == RX_Len)
SMART_CLEO 0:3f43ed494764 76 {
SMART_CLEO 0:3f43ed494764 77 RX_Status++;
SMART_CLEO 0:3f43ed494764 78 RX_count = 0;
SMART_CLEO 0:3f43ed494764 79 RX_Len = 32;
SMART_CLEO 0:3f43ed494764 80 }
SMART_CLEO 0:3f43ed494764 81 break;
SMART_CLEO 0:3f43ed494764 82 case 5:
SMART_CLEO 0:3f43ed494764 83 if(rx_da == 0x3E)
SMART_CLEO 0:3f43ed494764 84 {
SMART_CLEO 0:3f43ed494764 85 RX_BUF.END = rx_da;
SMART_CLEO 0:3f43ed494764 86 RX_Status = 0;
SMART_CLEO 0:3f43ed494764 87 switch(RX_BUF.MODE)
SMART_CLEO 0:3f43ed494764 88 {
SMART_CLEO 0:3f43ed494764 89 case 0x01:
SMART_CLEO 0:3f43ed494764 90 if(RX_BUF.CMD == 0x17)
SMART_CLEO 0:3f43ed494764 91 {
SMART_CLEO 0:3f43ed494764 92 char_data_write(RX_BUF.DATA[0]);
SMART_CLEO 0:3f43ed494764 93 }
SMART_CLEO 0:3f43ed494764 94 break;
SMART_CLEO 0:3f43ed494764 95 }
SMART_CLEO 0:3f43ed494764 96 }
SMART_CLEO 0:3f43ed494764 97 break;
SMART_CLEO 0:3f43ed494764 98 }
SMART_CLEO 0:3f43ed494764 99 }
SMART_CLEO 0:3f43ed494764 100
SMART_CLEO 0:3f43ed494764 101 void data_write(char *data, int len)
SMART_CLEO 0:3f43ed494764 102 {
SMART_CLEO 0:3f43ed494764 103 data[0] = 0;
SMART_CLEO 0:3f43ed494764 104 Dotmatrix.write(DoT_ADDR, data, len);
SMART_CLEO 0:3f43ed494764 105 }
SMART_CLEO 0:3f43ed494764 106
SMART_CLEO 0:3f43ed494764 107 void Command_Write(char com)
SMART_CLEO 0:3f43ed494764 108 {
SMART_CLEO 0:3f43ed494764 109 Dotmatrix.write(DoT_ADDR, &com, 1);
SMART_CLEO 0:3f43ed494764 110 }
SMART_CLEO 0:3f43ed494764 111
SMART_CLEO 0:3f43ed494764 112 void Dotmatrix_init(void)
SMART_CLEO 0:3f43ed494764 113 {
SMART_CLEO 0:3f43ed494764 114 // Internal System Clock enable
SMART_CLEO 0:3f43ed494764 115 Command_Write((char)0x21);
SMART_CLEO 0:3f43ed494764 116 // INT/ROW output pin Set -> ROW Driver output
SMART_CLEO 0:3f43ed494764 117 Command_Write((char)0xA0);
SMART_CLEO 0:3f43ed494764 118 // Dimming Set -> 15
SMART_CLEO 0:3f43ed494764 119 Command_Write((char)0xEF);
SMART_CLEO 0:3f43ed494764 120 // Blinking Set -> off
SMART_CLEO 0:3f43ed494764 121 // Display Set -> on
SMART_CLEO 0:3f43ed494764 122 Command_Write((char)0x81);
SMART_CLEO 0:3f43ed494764 123 }
SMART_CLEO 0:3f43ed494764 124
SMART_CLEO 0:3f43ed494764 125 void char_data_write(char data)
SMART_CLEO 0:3f43ed494764 126 {
SMART_CLEO 0:3f43ed494764 127 uint8_t read_c, i, j, c_buff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
SMART_CLEO 0:3f43ed494764 128 for(i=0; i<5; i++)
SMART_CLEO 0:3f43ed494764 129 {
SMART_CLEO 0:3f43ed494764 130 read_c = font[data-0x20][i];
SMART_CLEO 0:3f43ed494764 131 if(read_c)
SMART_CLEO 0:3f43ed494764 132 {
SMART_CLEO 0:3f43ed494764 133 for(j=0; j<8; j++)
SMART_CLEO 0:3f43ed494764 134 {
SMART_CLEO 0:3f43ed494764 135 if(read_c & (0x80 >> j))
SMART_CLEO 0:3f43ed494764 136 {
SMART_CLEO 0:3f43ed494764 137 c_buff[j] |= (0x01 << (i+1));
SMART_CLEO 0:3f43ed494764 138 }
SMART_CLEO 0:3f43ed494764 139 }
SMART_CLEO 0:3f43ed494764 140 }
SMART_CLEO 0:3f43ed494764 141 }
SMART_CLEO 0:3f43ed494764 142 for(i=0; i<8; i++)
SMART_CLEO 0:3f43ed494764 143 Data_BUF[i*2+1] = c_buff[i];
SMART_CLEO 0:3f43ed494764 144
SMART_CLEO 0:3f43ed494764 145 data_write(Data_BUF, 17);
SMART_CLEO 0:3f43ed494764 146 }