Skip to content

Commit 73f068a

Browse files
committed
add first lcd example
1 parent 7ffd949 commit 73f068a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lcdscreen_adc/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module lcdscreen
2+
3+
go 1.16
4+
5+
require tinygo.org/x/drivers v0.15.1

lcdscreen_adc/go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
2+
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
3+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
5+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
6+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8+
tinygo.org/x/drivers v0.15.1 h1:fS85Q6CVn1qcSF+n0JN61a5AULrbenjH5XIoYnQjOO4=
9+
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=

lcdscreen_adc/sense.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Package lia_lcd shows simple use of tinygo
2+
// to read Arduino Uno A0 pin and print value to LCD screen (hd44780, 4 bit config).
3+
// The arduino LED will blink as well.
4+
//
5+
// LCD connection scheme is same as shown at https://www.arduino.cc/en/Tutorial/LibraryExamples/HelloWorld
6+
//
7+
// E: Pin11, RS: Pin12, RW: NoPin
8+
package main
9+
10+
import (
11+
"machine"
12+
"strconv"
13+
"time"
14+
15+
// If tinygo errors when flashing due to package import error, install Go version 1.15.8, run go mod list (generate the go mod file) and reinstall Go 1.16 to be able to flash
16+
"tinygo.org/x/drivers/hd44780"
17+
)
18+
19+
const maxUint16 uint16 = 0xffff
20+
21+
// Arduino pins to LCD Dx pins 4 thru 7
22+
var Dpins = [4]machine.Pin{
23+
// Pins 4..7 on LCD are hooked to pins 5..2 on arduino board.
24+
machine.D5, machine.D4, machine.D3, machine.D2,
25+
}
26+
27+
// Potentiometer pin shall be configured to be an ADC. We use pin A0
28+
var pote = machine.ADC{
29+
Pin: machine.ADC0,
30+
}
31+
32+
func main() {
33+
// ADC configuration
34+
machine.InitADC()
35+
adcCfg := machine.ADCConfig{}
36+
pote.Configure(adcCfg)
37+
38+
// LED will blink. Thus we configure it to be an output.
39+
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
40+
41+
// LCD configs
42+
lcd, _ := hd44780.NewGPIO4Bit(
43+
Dpins[:],
44+
machine.D11, machine.D12, machine.NoPin,
45+
)
46+
lcd.Configure(hd44780.Config{
47+
Width: 16,
48+
Height: 2,
49+
CursorOnOff: false,
50+
CursorBlink: false,
51+
})
52+
53+
// Write tool versions at the time of writing this program
54+
lcd.ClearDisplay()
55+
lcd.Write([]byte("TinyGo0.17 Go16"))
56+
lcd.Display()
57+
58+
var val uint8
59+
for {
60+
// set line place to overwrite previous content
61+
lcd.SetCursor(0, 1)
62+
// read and print adc value
63+
val = adcToPcnt(pote.Get())
64+
lcd.Write([]byte(strconv.Itoa(int(val))))
65+
lcd.Display()
66+
// Blinky. On Arduino uno machine.LED is pin 13. (machine package specifies microchip pins, these may or may not coincide with board pin names)
67+
machine.LED.High()
68+
time.Sleep(time.Millisecond * 200)
69+
machine.LED.Low()
70+
time.Sleep(time.Millisecond * 200)
71+
}
72+
}
73+
74+
// Converts ADC uint16 value to a percent (uint8).
75+
// As of creating this program there is no full support of
76+
// 32bit floating point operations on AVR boards (Atmega boards mainly)
77+
func adcToPcnt(v uint16) uint8 {
78+
const div uint16 = maxUint16 / 100
79+
return uint8(v / div)
80+
}

0 commit comments

Comments
 (0)