diff --git a/Makefile b/Makefile index 7d5678e..b5b61fd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/arduinolib.c b/arduinolib.c index 1b2e743..586433e 100644 --- a/arduinolib.c +++ b/arduinolib.c @@ -2,60 +2,152 @@ #include #include #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<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; }*/ + diff --git a/arduinolib.epi b/arduinolib.epi index fc4a16f..0c0a718 100644 --- a/arduinolib.epi +++ b/arduinolib.epi @@ -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 () diff --git a/arduinolib.h b/arduinolib.h index 74ec448..0674500 100644 --- a/arduinolib.h +++ b/arduinolib.h @@ -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 *);*/ \ No newline at end of file diff --git a/arduinolib_types.h b/arduinolib_types.h index 0570697..ff409a4 100644 --- a/arduinolib_types.h +++ b/arduinolib_types.h @@ -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 \ No newline at end of file diff --git a/lcd_c/.DS_Store b/lcd_c/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/lcd_c/.DS_Store differ diff --git a/lcd_c/Makefile b/lcd_c/Makefile new file mode 100644 index 0000000..f20a956 --- /dev/null +++ b/lcd_c/Makefile @@ -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 + + diff --git a/lcd_c/lcd.c b/lcd_c/lcd.c new file mode 100644 index 0000000..6cf120b --- /dev/null +++ b/lcd_c/lcd.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; +} + diff --git a/lcd_c/lcd_definitions.h b/lcd_c/lcd_definitions.h new file mode 100644 index 0000000..fc3823e --- /dev/null +++ b/lcd_c/lcd_definitions.h @@ -0,0 +1,68 @@ +#include +#include + +/* 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 *); diff --git a/parallel_led/.DS_Store b/parallel_led/.DS_Store new file mode 100644 index 0000000..97842b3 Binary files /dev/null and b/parallel_led/.DS_Store differ diff --git a/parallel_led/Makefile b/parallel_led/Makefile new file mode 100644 index 0000000..7d5678e --- /dev/null +++ b/parallel_led/Makefile @@ -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 + + diff --git a/parallel_led/arduinolib.c b/parallel_led/arduinolib.c new file mode 100644 index 0000000..2ab4aa3 --- /dev/null +++ b/parallel_led/arduinolib.c @@ -0,0 +1,32 @@ +//#include +#include +#include +#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< +#include +#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; +} \ No newline at end of file diff --git a/parallel_led/main.h b/parallel_led/main.h new file mode 100644 index 0000000..7269ffa --- /dev/null +++ b/parallel_led/main.h @@ -0,0 +1,4 @@ +#ifndef MAIN_H +#define MAIN_H +#include "prog_c/prog.h" +#endif diff --git a/parallel_led/prog.ept b/parallel_led/prog.ept new file mode 100644 index 0000000..47175e7 --- /dev/null +++ b/parallel_led/prog.ept @@ -0,0 +1,24 @@ +open Arduinolib + +node led<>() 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 + diff --git a/prog.ept b/prog.ept index e349a16..19088e4 100644 --- a/prog.ept +++ b/prog.ept @@ -1,24 +1,10 @@ open Arduinolib -node led<>() 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