|
| 1 | +/** |
| 2 | + Arduino timer1 10-bit PWM |
| 3 | + v. 1.0 |
| 4 | + Copyright (C) 2020 Robert Ulbricht |
| 5 | + https://www.arduinoslovakia.eu |
| 6 | +
|
| 7 | + IDE: 1.8.12 or higher |
| 8 | + Board: Arduino Pro Mini |
| 9 | +
|
| 10 | + Libraries: |
| 11 | + U8g2: https://github.com/olikraus/u8g2 |
| 12 | + Version: 2.27.6 or higher |
| 13 | +
|
| 14 | + OLED | Arduino Pro Mini |
| 15 | + -----|----------------- |
| 16 | + GND | GND | |
| 17 | + VCC | VCC | |
| 18 | + CLK | 13 | |
| 19 | + MOSI | 11 | |
| 20 | + RES | 8 | |
| 21 | + DC | 6 | |
| 22 | + CS | 10 | |
| 23 | +
|
| 24 | + This program is free software: you can redistribute it and/or modify |
| 25 | + it under the terms of the GNU General Public License as published by |
| 26 | + the Free Software Foundation, either version 3 of the License, or |
| 27 | + (at your option) any later version. |
| 28 | +
|
| 29 | + This program is distributed in the hope that it will be useful, |
| 30 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | + GNU General Public License for more details. |
| 33 | +
|
| 34 | + You should have received a copy of the GNU General Public License |
| 35 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 36 | +*/ |
| 37 | + |
| 38 | +#include <U8g2lib.h> |
| 39 | +#include <util/atomic.h> |
| 40 | + |
| 41 | +U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 6, /* reset=*/ 8); |
| 42 | + |
| 43 | +#define ledPin 9 |
| 44 | + |
| 45 | +volatile int pwmvalue = 0; |
| 46 | +char buff[20]; |
| 47 | + |
| 48 | +// Mode 7: 10-bit Fast PWM |
| 49 | +void setupTimer1() { |
| 50 | + pinMode(ledPin, OUTPUT); |
| 51 | + TCCR1A = (1 << WGM11) | (1 << WGM10) | (1 << COM1A1); |
| 52 | + TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); |
| 53 | + OCR1A = 1; |
| 54 | +} |
| 55 | + |
| 56 | +void setup() { |
| 57 | + u8g2.begin(); |
| 58 | + u8g2.setFont(u8g2_font_ncenB10_tr); |
| 59 | + |
| 60 | + setupTimer1(); |
| 61 | +} |
| 62 | + |
| 63 | +void drawInfo() { |
| 64 | + int y = 12; |
| 65 | + u8g2.firstPage(); |
| 66 | + do { |
| 67 | + |
| 68 | + u8g2.drawStr(0, y, "Arduino Slovakia"); |
| 69 | + sprintf(buff, "PWM: %d", pwmvalue); |
| 70 | + u8g2.drawStr(0, y * 3, buff); |
| 71 | + } while ( u8g2.nextPage() ); |
| 72 | +} |
| 73 | + |
| 74 | +void loop() { |
| 75 | + long v = 0; |
| 76 | + for (int i = 0; i < 64; i++) |
| 77 | + v += analogRead(A0); |
| 78 | + pwmvalue = v / 64; |
| 79 | + OCR1A = pwmvalue; |
| 80 | + drawInfo(); |
| 81 | +} |
0 commit comments