MSP device support complete. NOT TESTED YET! Preliminary python host lib
[cerebrum.git] / avr / 7seg.c
blobba7dd5028aa20f1d2782baa65a20b28ac06c3ce7
1 #include "config.h"
2 #ifdef HAS_7SEG_SUPPORT
3 #include <avr/io.h>
4 #include <util/delay.h>
5 #include <avr/interrupt.h>
6 #include <avr/pgmspace.h>
7 #include "uart.h"
8 #include "7seg.h"
10 void l7seg_setup(){
11 L7SEG_CLK_DDR |= _BV(L7SEG_CLK_PIN);
12 L7SEG_DATA_DDR |= _BV(L7SEG_DATA_PIN);
13 L7SEG_DIGIT1_DDR |= _BV(L7SEG_DIGIT1_PIN);
14 L7SEG_DIGIT2_DDR |= _BV(L7SEG_DIGIT2_PIN);
17 inline void l7seg_pulse_clk(void){
18 L7SEG_CLK_PORT |= _BV(L7SEG_CLK_PIN);
19 L7SEG_CLK_PORT &= ~_BV(L7SEG_CLK_PIN);
22 inline void l7seg_data_out(uint8_t val){
23 L7SEG_DATA_PORT &= ~_BV(L7SEG_DATA_PIN);
24 if(!val){
25 L7SEG_DATA_PORT |= _BV(L7SEG_DATA_PIN);
29 inline void l7seg_select_digit(uint8_t digit){
30 L7SEG_DIGIT1_PORT &= ~_BV(L7SEG_DIGIT1_PIN);
31 L7SEG_DIGIT2_PORT &= ~_BV(L7SEG_DIGIT2_PIN);
32 if(digit&1){
33 L7SEG_DIGIT1_PORT |= _BV(L7SEG_DIGIT1_PIN);
35 if(digit&2){
36 L7SEG_DIGIT2_PORT |= _BV(L7SEG_DIGIT2_PIN);
40 int l7seg_cycle=1;
42 int l7seg_digit1[] = {
43 0x56C0, //0
44 0x0600, //1
45 0x94C0, //2
46 0x9640, //3
47 0xC600, //4
48 0xD240, //5
49 0xD2C0, //6
50 0x1600, //7
51 0xD6C0, //8
52 0xD640, //9
53 0xD680, //A
54 0xC2C0, //B
55 0x50C0, //C
56 0x86C0, //D
57 0xD0C0, //E
58 0xD080 //F
61 int l7seg_digit2[] = {
62 0x011F,
63 0x000C,
64 0x081B,
65 0x081E,
66 0x090C,
67 0x0916,
68 0x0917,
69 0x001C,
70 0x091F,
71 0x091E,
72 0x091D,
73 0x0907,
74 0x0113,
75 0x080F,
76 0x0913,
77 0x0911
80 int l7seg_get_digit(uint8_t b, uint8_t a){
81 int out = 0;
82 a -= '0';
83 b -= '0';
84 if(a > 0x20)
85 a-=0x20;
86 if(a > 0x10)
87 a-=7;
88 if(b > 0x20)
89 b-=0x20;
90 if(b > 0x10)
91 b-=7;
92 out = l7seg_digit1[a];
93 return out | l7seg_digit2[b];
96 void l7seg_loop(){ //one frame
97 uint8_t nbuf = 0;
98 static uint8_t pos = 0;
99 if(pos&1){
100 l7seg_cycle = l7seg_get_digit(l7seg_buf[2],l7seg_buf[3]);
101 l7seg_select_digit(0);
102 for(uint8_t i = 0; i<16; i++){
103 l7seg_data_out(l7seg_cycle&1);
104 l7seg_cycle>>=1;
105 l7seg_pulse_clk();
107 l7seg_data_out(0);
108 l7seg_select_digit(1);
109 }else{
110 l7seg_cycle = l7seg_get_digit(l7seg_buf[0],l7seg_buf[1]);
111 l7seg_select_digit(0);
112 for(uint8_t i = 0; i<16; i++){
113 l7seg_data_out(l7seg_cycle&1);
114 l7seg_cycle>>=1;
115 l7seg_pulse_clk();
117 l7seg_data_out(0);
118 l7seg_select_digit(2);
120 pos++;
123 uint8_t _l7seg_buf[4] = {'1','3','3','7'};
124 uint8_t* l7seg_buf = _l7seg_buf;
126 #else//HAS_7SEG_SUPPORT
128 void l7seg_setup(void){}
129 void l7seg_loop(void){}
131 #endif//HAS_7SEG_SUPPORT