projects:blinker:work_logs:7_code
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projects:blinker:work_logs:7_code [2022/02/28 12:44] – tjhowse | projects:blinker:work_logs:7_code [2022/03/03 07:17] (current) – tjhowse | ||
---|---|---|---|
Line 16: | Line 16: | ||
I wrote a [[https:// | I wrote a [[https:// | ||
- | First we take an input message, like `sos` and translate it to Morse code: | + | First we take an input message, like **sos** and translate it to Morse code: |
< | < | ||
Line 24: | Line 24: | ||
</ | </ | ||
- | According to the Morse code spec a `dit` is the shortest time window. The gap between tones (or keys, or flashes) is the same length as a `dit`. A `dah` is three times that length. The gap between letters is the same length as a `dah`. The gap between words is seven times the length of a `dit`. Taking that all into account we can map a `1` or a `0` to whether the LED should be on at a given point in time: | + | According to the Morse code spec a **dit** is the shortest time window. The gap between tones (or keys, or flashes) is the same length as a **dit**. A **dah** is three times that length. The gap between letters is the same length as a **dah**. The gap between words is seven times the length of a **dit**. Taking that all into account we can map a **1** or a **0** to whether the LED should be on at a given point in time: |
< | < | ||
Line 34: | Line 34: | ||
<code c> | <code c> | ||
- | uint8_t const sequence[] = {0x15, 0x77, 0x47, 0x05, 0x5c, 0x5c, 0xc5, 0x05}; | + | uint8_t const sequence[] = {0x15, 0x77, 0x47, 0x05, 0x00}; |
</ | </ | ||
Line 52: | Line 52: | ||
</ | </ | ||
+ | There is some bit-shifty nonsense going on to pick the correct bit out of the sequence. Then we either turn the LED on or off based on the value of the current bit of interest. Then we increment the counter and wrap it back to zero if it went past the end of the sequence. | ||
+ | ==== Power saving ==== | ||
+ | The Attiny10 has some options for reducing its power consumption. We can shut down the analogue to digital converter (ADC) since we're not reading any analogue values, and we can stop TIMER0 as we won't be using it. | ||
+ | <code c> | ||
+ | // Disable ADC: | ||
+ | ADCSRA = 0; | ||
+ | // Shut down ADC and timer0 | ||
+ | PRR = 0b11; | ||
+ | </ | ||
+ | The biggest power saving came from putting the microcontroller to sleep between " | ||
+ | <code c> | ||
+ | void sleep() { | ||
+ | // Tell the watchdog timer to wake us up in 120ms. | ||
+ | wdt_enable(WDTO_120MS); | ||
+ | WDTCSR |= (1<< | ||
+ | // Go to sleep. | ||
+ | set_sleep_mode(SLEEP_MODE_PWR_DOWN); | ||
+ | sleep_enable(); | ||
+ | sleep_cpu(); | ||
+ | } | ||
+ | // This fires when the watchdog timer expires and wakes up the CPU. | ||
+ | ISR(WDT_vect) { | ||
+ | sleep_disable(); | ||
+ | } | ||
+ | </ | ||
+ | This code is called after the **blink()** function above. It recruits the watchdog timer to fire an interrupt event in 120ms, then goes to sleep. This stops everything in the CPU except the watchdog timer. The contents of RAM are conserved. When the watchdog timer goes off it calls the interrupt service routine (ISR) which disables sleep mode. Then **blink()** fires again and we go back to sleep. | ||
+ | This approach reduced power consumption by the CPU and LED [[projects: | ||
+ | ==== Testing ==== | ||
- | ===== Video ===== | + | I don't (yet) know how to read Morse code, but I wanted to validate whether I was blinking out the message correctly, and looping around at the end. I wrote some [[https:// |
- | {{ youtube> | + | {{: |
- | ===== Next Time ===== | + | It worked really well! I fed some [[https:// |
+ | |||
+ | < | ||
+ | lorem ipsum dolor sit amet consectetur adipiscing elit donec faucibus | ||
+ | quis iaculis volutpat | ||
+ | est fusce elementum vitae risus non aliquam | ||
+ | scelerisque orci egestas | ||
+ | pellentesque lobortis | ||
+ | sagittis congue massa quis tincidunt felis nunc et orci eget mi mollis viverra | ||
+ | non in nunc nullam dapibus pulvinar lectus | ||
+ | tellus id enim posuere sagittis | ||
+ | qwerty asdfg zxcvb | ||
+ | </ | ||
+ | |||
+ | Switching from a Logitech C922 Pro to a PS3 eye let me reduce the dit interval to the final value of 125ms and still decode it perfectly. Fun! | ||
+ | |||
+ | ===== Video ===== | ||
- | I don't think there' | + | {{ youtube> |
[<6>] | [<6>] | ||
projects/blinker/work_logs/7_code.1646052255.txt.gz · Last modified: 2022/02/28 12:44 by tjhowse