code lcd in c and with heptagon

master
MegiDervishi 3 years ago
parent ad83ae83e4
commit e4e2aa095e

@ -2,7 +2,7 @@ CC = avr-gcc
OBJCOPY = avr-objcopy
AVRDUDE = avrdude
CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p
SERIAL = /dev/cu.usbmodem143401
SERIAL = /dev/cu.usbmodem141401
ADDR = $(shell heptc -where)/c
TARGET = prog
LIB = arduinolib

@ -2,60 +2,152 @@
#include <avr/io.h>
#include <util/delay.h>
#include "arduinolib.h"
//#include "HardwareSerial.h"
void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out){
/* set pin 5 of PORTB for output. i.e port 13 */
if (p == 13){
DDRB |= _BV(DDB5);
if (v == 0)
{
PORTB &= ~(1<<PORTB5);
//PORTB &= ~_BV(PORTB5); //put it to low
return;
}
PORTB |= (1<<PORTB5);
//PORTB |= _BV(PORTB5);
return;
}
DDRB |= _BV(DDB4);
if (v == 0)
{
PORTB &= ~(1<<PORTB4);
//PORTB &= ~_BV(PORTB4); //put it to low
return;
}
PORTB |= (1<<PORTB4);
//PORTB |= _BV(PORTB4);
return;
/* Configure the pins of arduino */
void Arduinolib__dinit_step(Arduinolib__dinit_out *out){
//control
LCD_E_DDR |= (1 << LCD_E_PIN);
LCD_RS_DDR |= (1 << LCD_RS_PIN);
//data
LCD_D4_DDR |= (1 << LCD_D4_PIN);
LCD_D5_DDR |= (1 << LCD_D5_PIN);
LCD_D6_DDR |= (1 << LCD_D6_PIN);
LCD_D7_DDR |= (1 << LCD_D7_PIN);
}
/* Initialize the LCD as described in the datasheet */
void Arduinolib__lcd_init_step(Arduinolib__lcd_init_out *out){
_delay_ms(50); //power delay more than 15ms
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //RS to low
LCD_E_PORT &= ~(1 << LCD_E_PIN); //E to low
// "reboot process"
Arduinolib__lcd_write_out *o1;
Arduinolib__lcd_write_step(LCD_FUNCTION_SET_8bit, o1);
_delay_ms(10); //more than 4.1ms
Arduinolib__lcd_write_out *o2;
Arduinolib__lcd_write_step(LCD_FUNCTION_SET_8bit,o2);
_delay_us(200); //more than 100us
Arduinolib__lcd_write_out *o3;
Arduinolib__lcd_write_step(LCD_FUNCTION_SET_8bit, o3);
_delay_us(200); //more than 100us
//set to 4bit
Arduinolib__lcd_write_out *o4;
Arduinolib__lcd_write_step(LCD_FUNCTION_SET_4bit, o4);
_delay_us(80); //more than 39us
Arduinolib__lcd_cmd_out *o5;
Arduinolib__lcd_cmd_step(LCD_FUNCTION_SET_4bit, o5);
_delay_us(80); //more than 39us
//send instructions
Arduinolib__lcd_cmd_out *o6;
Arduinolib__lcd_cmd_step(LCD_DISPLAY_OFF, o6);
_delay_us(80); //more than 39us
Arduinolib__lcd_cmd_out *o7;
Arduinolib__lcd_cmd_step(LCD_CLEAR, o7);
_delay_ms(3); //more than 1.53 ms
Arduinolib__lcd_cmd_out *o8;
Arduinolib__lcd_cmd_step(LCD_ENTRY_MODE, o8);
_delay_us(80); //more than 39us
Arduinolib__lcd_cmd_out *o9;
Arduinolib__lcd_cmd_step(LCD_DISPLAY_ON, o9);
_delay_us(80); //more than 39us
// if we use this code the msg is printed ///
/*uint8_t msg[] = "Arduino";
Arduinolib__lcd_string_out *o;
Arduinolib__lcd_string_step(msg, o);*/
}
/* send an 8bit information to the LCD register in order to be written */
void Arduinolib__lcd_write_step(uint8_t info, Arduinolib__lcd_write_out *out){
//(info & 1 << 4) ? LCD_D4_PORT |= (1 << LCD_D4_PIN) : LCD_D4_PORT &= ~(1 << LCD_D4_PIN)
if (info & 1<<4){
LCD_D4_PORT |= (1 << LCD_D4_PIN);
}
else {
LCD_D4_PORT &= ~(1 << LCD_D4_PIN);
}
if (info & 1<<5){
LCD_D5_PORT |= (1 << LCD_D5_PIN);
}
else {
LCD_D5_PORT &= ~(1 << LCD_D5_PIN);
}
if (info & 1<<6){
LCD_D6_PORT |= (1 << LCD_D6_PIN);
}
else {
LCD_D6_PORT &= ~(1 << LCD_D6_PIN);
}
if (info & 1<<7){
LCD_D7_PORT |= (1 << LCD_D7_PIN);
}
else {
LCD_D7_PORT &= ~(1 << LCD_D7_PIN);
}
//pulse the Enable pin to indicate we have written the data
LCD_E_PORT |= (1 << LCD_E_PIN); //high
_delay_us(1);
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
_delay_us(1);
}
/* send an 8bit instruction to the LCD instruction register */
void Arduinolib__lcd_cmd_step(uint8_t cmd, Arduinolib__lcd_cmd_out *out){
/*void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out)
{
pinMode(p,OUTPUT);
if(v==0)
{
digitalWrite(p,LOW);
//Serial.write("low");
return;
}
digitalWrite(p,HIGH);
//Serial.write("high");
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //low
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
Arduinolib__lcd_write_out *o1;
Arduinolib__lcd_write_step(cmd, o1); // upper 4 bits
Arduinolib__lcd_write_out *o2;
Arduinolib__lcd_write_step(cmd << 4, o2); // lower 4 bits
}
*/
/*void Mathext__st_cos_reset(Mathext__st_cos_mem *self)
{
int j;
self->i = 0;
for(j = 0; j < 100; ++j)
self->mem[j] = 0.0;
/* send an 8bit data character to the LCD register */
void Arduinolib__lcd_char_step(uint8_t char_data, Arduinolib__lcd_char_out *out){
LCD_RS_PORT |= (1 << LCD_RS_PIN); //high
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
Arduinolib__lcd_write_out *o1;
Arduinolib__lcd_write_step(char_data, o1); // upper 4 bits
Arduinolib__lcd_write_out *o2;
Arduinolib__lcd_write_step(char_data << 4, o2); // lower 4 bits
}
/* give a string to display to the LCD */
void Arduinolib__lcd_string_step(uint8_t lstring[], Arduinolib__lcd_string_out *out){
//uint8_t lstring = atoi(message);
volatile int i = 0;
for (i = 0; lstring[i] != 0 ; i++) {
Arduinolib__lcd_char_out *o1;
Arduinolib__lcd_char_step(lstring[i], o1);
_delay_us(80); //more than 39us
}
}
void Mathext__st_cos_step(float a, Mathext__st_cos_out *out, Mathext__st_cos_mem *self)
{
out->o = self->mem[self->i];
self->i = (self->i+1) % 100;
self->mem[self->i] = cosf(a);
/*uint8_t msg[] = "Arduino";
uint8_t msg2[] = "Hello world";
int main(void) {
Arduinolib__dinit_step();
Arduinolib__lcd_init_step();
Arduinolib__lcd_string_step(msg);
Arduinolib__lcd_cmd_step(LCD_CURSOR_SET | LCD_START_LINE2);
_delay_us(80); // more than 39us
Arduinolib__lcd_string_step(msg2);
while(1);
return 0;
}*/

@ -1,2 +1,8 @@
fun dwrite(p:int; v:bool) returns ()
type uint8_t = int
fun dinit() returns ()
fun lcd_init() returns ()
fun lcd_write(info:uint8_t) returns ()
fun lcd_cmd(cmd:uint8_t) returns ()
fun lcd_char(char_data:uint8_t) returns ()
fun lcd_string (lstring:uint8_t) returns ()

@ -1 +1,67 @@
#include "arduinolib_types.h"
/* LCD with 4 bit mode for an ATMEGA32P processor
_ LCD _ _ ATMEGA32P (arduino)_
1 VSS -> GND
2 VDD -> +5V
3 V0 -> Potentiometer
4 RS -> PortB4 (Pin 12 arduino)
5 RW -> GND
6 E -> PortB3 (Pin 11 arduino)
(7 - 10) -> ...
11 D4 -> PortD5 (Pin 5 arduino)
12 D5 -> PortD4 (Pin 4 arduino)
13 D6 -> PortD3 (Pin 3 arduino)
14 D7 -> PortD2 (Pin 2 arduino)
15 A -> +5v with resistor
16 K -> GND
*/
// LCD display size
#define LCD_START_LINE1 0x00
#define LCD_START_LINE2 0x40
// LCD ports/pins/ddr
#define LCD_RS_PORT PORTB
#define LCD_RS_PIN 4
#define LCD_RS_DDR DDRB
#define LCD_E_PORT PORTB
#define LCD_E_PIN 3
#define LCD_E_DDR DDRB
#define LCD_D4_PORT PORTD
#define LCD_D4_PIN 5
#define LCD_D4_DDR DDRD
#define LCD_D5_PORT PORTD
#define LCD_D5_PIN 4
#define LCD_D5_DDR DDRD
#define LCD_D6_PORT PORTD
#define LCD_D6_PIN 3
#define LCD_D6_DDR DDRD
#define LCD_D7_PORT PORTD
#define LCD_D7_PIN 2
#define LCD_D7_DDR DDRD
//LCD instructions (see LCD-1602A datasheet)
#define LCD_CLEAR 0b00000001
#define LCD_HOME 0b00000010
#define LCD_ENTRY_MODE 0b00000110
#define LCD_DISPLAY_ON 0b00001100
#define LCD_DISPLAY_OFF 0b00001000
#define LCD_CURSOR_SET 0b10000000
#define LCD_FUNCTION_SET_8bit 0b00110000
#define LCD_FUNCTION_SET_4bit 0b00101000
// Functions
/*void dinit(void);
void lcd_init(void);
void lcd_write(uint8_t);
void lcd_cmd(uint8_t);
void lcd_char(uint8_t);
void lcd_string(uint8_t *);*/

@ -1,11 +1,44 @@
#ifndef ARDUINOLIB_H
#define ARDUINOLIB_H
//#include "Arduino.h"
//#include "WConstants.h"
/* Example of a combinatorial function */
typedef struct Arduinolib__dwrite {
} Arduinolib__dwrite_out;
void Arduinolib__dwrite_step(int p, int v,Arduinolib__dwrite_out *o);
//typedef struct string {
// const char *msg
//} Arduinolib__string_out;
#endif
typedef struct byte_out {
uint8_t value ;
} byte_out ;
typedef byte_out Arduinolib__bitOr_out ;
typedef struct Arduinolib__dinit {
} Arduinolib__dinit_out;
typedef struct Arduinolib__lcd_init {
} Arduinolib__lcd_init_out;
typedef struct Arduinolib__lcd_write {
} Arduinolib__lcd_write_out;
typedef struct Arduinolib__lcd_cmd {
} Arduinolib__lcd_cmd_out;
typedef struct Arduinolib__lcd_char {
} Arduinolib__lcd_char_out;
typedef struct Arduinolib__lcd_string {
} Arduinolib__lcd_string_out;
void Arduinolib__dinit_step(Arduinolib__dinit_out *o);
void Arduinolib__lcd_init_step(Arduinolib__lcd_init_out *o);
void Arduinolib__lcd_write_step(uint8_t info, Arduinolib__lcd_write_out *o);
void Arduinolib__lcd_cmd_step(uint8_t cmd, Arduinolib__lcd_cmd_out *o);
void Arduinolib__lcd_char_step(uint8_t char_data, Arduinolib__lcd_char_out *o);
void Arduinolib__lcd_string_step(uint8_t lstring[], Arduinolib__lcd_string_out *o);
#endif

BIN
lcd_c/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,37 @@
CC = avr-gcc
OBJCOPY = avr-objcopy
AVRDUDE = avrdude
CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p
SERIAL = /dev/cu.usbmodem141401
ADDR = $(shell heptc -where)/c
TARGET = lcd
.PHONY: all clean flash
all: $(TARGET).hex
flash: $(TARGET).hex
$(AVRDUDE) -F -V -c arduino -p ATMEGA328P -P $(SERIAL) -b 115200 \
-U flash:w:$<
$(TARGET).hex: $(TARGET)
$(OBJCOPY) -O ihex -R .eeprom $< $@
$(TARGET): $(TARGET).o
$(CC) $(CFLAGS) -o $@ *.o
$(TARGET).o : $(TARGET).c
$(CC) -g $(CFLAGS) -c -I $(ADDR) -I . $(TARGET).c
clean:
rm -f $(TARGET) $(TARGET).hex $(TARGET).o $(TARGET).log $(TARGET).mls
rm -f *.o *.epci *.hex
rm -r $(TARGET)_c

@ -0,0 +1,130 @@
#include "lcd_definitions.h"
/* Configure the pins of arduino */
void dinit(void){
//control
LCD_E_DDR |= (1 << LCD_E_PIN);
LCD_RS_DDR |= (1 << LCD_RS_PIN);
//data
LCD_D4_DDR |= (1 << LCD_D4_PIN);
LCD_D5_DDR |= (1 << LCD_D5_PIN);
LCD_D6_DDR |= (1 << LCD_D6_PIN);
LCD_D7_DDR |= (1 << LCD_D7_PIN);
}
/* Initialize the LCD as described in the datasheet */
void lcd_init(void){
_delay_ms(50); //power delay more than 15ms
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //RS to low
LCD_E_PORT &= ~(1 << LCD_E_PIN); //E to low
// "reboot process"
lcd_write(LCD_FUNCTION_SET_8bit);
_delay_ms(10); //more than 4.1ms
lcd_write(LCD_FUNCTION_SET_8bit);
_delay_us(200); //more than 100us
lcd_write(LCD_FUNCTION_SET_8bit);
_delay_us(200); //more than 100us
//set to 4bit
lcd_write(LCD_FUNCTION_SET_4bit);
_delay_us(80); //more than 39us
lcd_cmd(LCD_FUNCTION_SET_4bit);
_delay_us(80); //more than 39us
//send instructions
lcd_cmd(LCD_DISPLAY_OFF);
_delay_us(80); //more than 39us
lcd_cmd(LCD_CLEAR);
_delay_ms(3); //more than 1.53 ms
lcd_cmd(LCD_ENTRY_MODE);
_delay_us(80); //more than 39us
lcd_cmd(LCD_DISPLAY_ON);
_delay_us(80); //more than 39us
}
/* send an 8bit information to the LCD register in order to be written */
void lcd_write(uint8_t info){
//(info & 1 << 4) ? LCD_D4_PORT |= (1 << LCD_D4_PIN) : LCD_D4_PORT &= ~(1 << LCD_D4_PIN)
if (info & 1<<4){
LCD_D4_PORT |= (1 << LCD_D4_PIN);
}
else {
LCD_D4_PORT &= ~(1 << LCD_D4_PIN);
}
if (info & 1<<5){
LCD_D5_PORT |= (1 << LCD_D5_PIN);
}
else {
LCD_D5_PORT &= ~(1 << LCD_D5_PIN);
}
if (info & 1<<6){
LCD_D6_PORT |= (1 << LCD_D6_PIN);
}
else {
LCD_D6_PORT &= ~(1 << LCD_D6_PIN);
}
if (info & 1<<7){
LCD_D7_PORT |= (1 << LCD_D7_PIN);
}
else {
LCD_D7_PORT &= ~(1 << LCD_D7_PIN);
}
//pulse the Enable pin to indicate we have written the data
LCD_E_PORT |= (1 << LCD_E_PIN); //high
_delay_us(1);
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
_delay_us(1);
}
/* send an 8bit instruction to the LCD instruction register */
void lcd_cmd(uint8_t cmd){
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //low
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
lcd_write(cmd); // upper 4 bits
lcd_write(cmd << 4); // lower 4 bits
}
/* send an 8bit data character to the LCD register */
void lcd_char(uint8_t char_data){
LCD_RS_PORT |= (1 << LCD_RS_PIN); //high
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
lcd_write(char_data); // upper 4 bits
lcd_write(char_data << 4); // lower 4 bits
}
/* give a string to display to the LCD */
void lcd_string(uint8_t lstring[]){
//uint8_t lstring = atoi(message);
volatile int i = 0;
for (i = 0; lstring[i] != 0 ; i++) {
lcd_char(lstring[i]);
_delay_us(80); //more than 39us
}
}
uint8_t msg[] = "Arduino";
uint8_t msg2[] = "Hello world";
int main(void) {
dinit();
lcd_init();
lcd_string(msg);
lcd_cmd(LCD_CURSOR_SET | LCD_START_LINE2);
_delay_us(80); // more than 39us
lcd_string(msg2);
while(1);
return 0;
}

@ -0,0 +1,68 @@
#include <avr/io.h>
#include <util/delay.h>
/* LCD with 4 bit mode for an ATMEGA32P processor
_ LCD _ _ ATMEGA32P (arduino)_
1 VSS -> GND
2 VDD -> +5V
3 V0 -> Potentiometer
4 RS -> PortB4 (Pin 12 arduino)
5 RW -> GND
6 E -> PortB3 (Pin 11 arduino)
(7 - 10) -> ...
11 D4 -> PortD5 (Pin 5 arduino)
12 D5 -> PortD4 (Pin 4 arduino)
13 D6 -> PortD3 (Pin 3 arduino)
14 D7 -> PortD2 (Pin 2 arduino)
15 A -> +5v with resistor
16 K -> GND
*/
// LCD display size
#define LCD_START_LINE1 0x00
#define LCD_START_LINE2 0x40
// LCD ports/pins/ddr
#define LCD_RS_PORT PORTB
#define LCD_RS_PIN 4
#define LCD_RS_DDR DDRB
#define LCD_E_PORT PORTB
#define LCD_E_PIN 3
#define LCD_E_DDR DDRB
#define LCD_D4_PORT PORTD
#define LCD_D4_PIN 5
#define LCD_D4_DDR DDRD
#define LCD_D5_PORT PORTD
#define LCD_D5_PIN 4
#define LCD_D5_DDR DDRD
#define LCD_D6_PORT PORTD
#define LCD_D6_PIN 3
#define LCD_D6_DDR DDRD
#define LCD_D7_PORT PORTD
#define LCD_D7_PIN 2
#define LCD_D7_DDR DDRD
//LCD instructions (see LCD-1602A datasheet)
#define LCD_CLEAR 0b00000001
#define LCD_HOME 0b00000010
#define LCD_ENTRY_MODE 0b00000110
#define LCD_DISPLAY_ON 0b00001100
#define LCD_DISPLAY_OFF 0b00001000
#define LCD_CURSOR_SET 0b10000000
#define LCD_FUNCTION_SET_8bit 0b00110000
#define LCD_FUNCTION_SET_4bit 0b00101000
// Functions
void dinit(void);
void lcd_init(void);
void lcd_write(uint8_t);
void lcd_cmd(uint8_t);
void lcd_char(uint8_t);
void lcd_string(uint8_t *);

Binary file not shown.

@ -0,0 +1,42 @@
CC = avr-gcc
OBJCOPY = avr-objcopy
AVRDUDE = avrdude
CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p
SERIAL = /dev/cu.usbmodem143401
ADDR = $(shell heptc -where)/c
TARGET = prog
LIB = arduinolib
MAIN = main
.PHONY: all clean flash
all: $(TARGET).hex
flash: $(TARGET).hex
$(AVRDUDE) -F -V -c arduino -p ATMEGA328P -P $(SERIAL) -b 115200 \
-U flash:w:$<
$(TARGET).hex: $(TARGET)
$(OBJCOPY) -O ihex -R .eeprom $< $@
$(TARGET): $(TARGET).o
$(CC) $(CFLAGS) -o $@ *.o
$(TARGET).o : $(TARGET).c
$(CC) -g $(CFLAGS) -c -I $(ADDR) -I . $(LIB).c $(TARGET)_c/*.c $(MAIN).c
$(TARGET).c:
heptc $(LIB).epi
heptc -target c $(TARGET).ept
clean:
rm -f $(TARGET) $(TARGET).hex $(TARGET).o $(TARGET).log $(TARGET).mls
rm -f *.o *.epci
rm -r $(TARGET)_c

@ -0,0 +1,32 @@
//#include <math.h>
#include <avr/io.h>
#include <util/delay.h>
#include "arduinolib.h"
//#include "HardwareSerial.h"
void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out){
/* set pin 5 of PORTB for output. i.e port 13 */
if (p == 13){
DDRC |= _BV(DDD7);
if (v == 0)
{
PORTD &= ~(1<<PORTD7);
//PORTB &= ~_BV(PORTB5); //put it to low
return;
}
PORTD |= (1<<PORTD7);
//PORTB |= _BV(PORTB5);
return;
}
DDRD |= _BV(DDD5);
if (v == 0)
{
PORTD &= ~(1<<PORTD5);
//PORTB &= ~_BV(PORTB4); //put it to low
return;
}
PORTD |= (1<<PORTD5);
//PORTB |= _BV(PORTB4);
return;
}

@ -0,0 +1,2 @@
fun dwrite(p:int; v:bool) returns ()

@ -0,0 +1 @@
#include "arduinolib_types.h"

@ -0,0 +1,11 @@
#ifndef ARDUINOLIB_H
#define ARDUINOLIB_H
//#include "Arduino.h"
//#include "WConstants.h"
/* Example of a combinatorial function */
typedef struct Arduinolib__dwrite {
} Arduinolib__dwrite_out;
void Arduinolib__dwrite_step(int p, int v,Arduinolib__dwrite_out *o);
#endif

@ -0,0 +1,17 @@
#include <avr/io.h>
#include <util/delay.h>
#include "main.h"
//#define BLINK_DELAY_MS 4000
int main (void)
{
Prog__main_mem mem;
Prog__main_reset(&mem);
while(1){
Prog__main_out _res;
Prog__main_step(&_res, &mem);
_delay_ms(1000);
}
return 0;
}

@ -0,0 +1,4 @@
#ifndef MAIN_H
#define MAIN_H
#include "prog_c/prog.h"
#endif

@ -0,0 +1,24 @@
open Arduinolib
node led<<period, ledMax : int; opin : int>>() returns ()
var i : int;
ledTime : int;
upt : bool;
downt : bool;
let
i = 0 fby (i+1);
ledTime = i % period;
upt = ledTime < ledMax;
downt = not upt;
() = dwrite((opin, true) when upt);
() = dwrite((opin, false) when downt);
tel
node main() returns ()
let
() = led<<3, 1, 13>>();
() = led<<4, 2, 9>>();
tel

@ -1,24 +1,10 @@
open Arduinolib
node led<<period, ledMax : int; opin : int>>() returns ()
var i : int;
ledTime : int;
upt : bool;
downt : bool;
let
i = 0 fby (i+1);
ledTime = i % period;
upt = ledTime < ledMax;
downt = not upt;
() = dwrite((opin, true) when upt);
() = dwrite((opin, false) when downt);
tel
node main() returns ()
let
() = led<<4, 2, 13>>();
() = led<<3, 1, 9>>();
var i : int;
let
i = 0 fby (i+1);
() = dinit();
() = lcd_init();
tel

Loading…
Cancel
Save