Removed an unnecessary sleep from r0ketbeam. Added support for 7 segment
[cerebrum.git] / 7seg.c
blobdf4e94880c1d1747d86d82a00d14e419d648f23c
1 #include <avr/io.h>
2 #include <util/delay.h>
3 #include <avr/interrupt.h>
4 #include <avr/pgmspace.h>
5 #include "uart.h"
7 void 7seg_setup(){
8 DDRB=0x0F;
9 sei();
12 inline void 7seg_pulse_clk(){
13 PORTB |= 0x04;
14 PORTB &= 0xFB;
17 inline void 7seg_data_out(uint8_t val){
18 PORTB &= 0xF7;
19 if(!val){
20 PORTB |= 0x08;
24 inline void 7seg_select_digit(uint8_t digit){
25 PORTB &= 0xFC;
26 PORTB |= digit&3;
29 int 7seg_cycle=1;
31 int 7seg_digit1[] = {
32 0x56C0,
33 0x6000,
34 0x94C0,
35 0x9640,
36 0xC600,
37 0xD240,
38 0xD2C0,
39 0x1600,
40 0xD6C0,
41 0xD640,
42 0xD680,
43 0xC2C0,
44 0x50C0,
45 0x86C0,
46 0xD0C0,
47 0xD080
50 int 7seg_digit2[] = {
51 0x011F,
52 0x000C,
53 0x081B,
54 0x081E,
55 0x090C,
56 0x0916,
57 0x0917,
58 0x001C,
59 0x091F,
60 0x091E,
61 0x091D,
62 0x0907,
63 0x0113,
64 0x080F,
65 0x0913,
66 0x0911
69 int 7seg_get_digit(uint8_t b, uint8_t a){
70 int out = 0;
71 a -= '0';
72 b -= '0';
73 if(a > 0x20)
74 a-=0x20;
75 if(a > 0x10)
76 a-=7;
77 if(b > 0x20)
78 b-=0x20;
79 if(b > 0x10)
80 b-=7;
81 out = 7seg_digit1[a];
82 return out | 7seg_digit2[b];
85 void 7seg_loop(){ //one frame
86 cycle = get_digit(7seg_buf[2],7seg_buf[3]);
87 for(uint8_t i = 0; i<16; i++){
88 data_out(cycle&1);
89 cycle>>=1;
90 pulse_clk();
92 data_out(0);
93 select_digit(1);
94 _delay_ms(1);
95 select_digit(0);
96 cycle = get_digit(7seg_buf[0],7seg_buf[1]);
97 for(uint8_t i = 0; i<16; i++){
98 data_out(cycle&1);
99 cycle>>=1;
100 pulse_clk();
102 data_out(0);
103 select_digit(2);
104 _delay_ms(1);
105 select_digit(0);