[ADD] files to include

This commit is contained in:
Sem van der Hoeven
2021-03-31 09:47:58 +02:00
parent 9594f0e367
commit 956e25bc6a
4 changed files with 265 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
/*
* lcd_controlc.c
*
* Created: 24-2-2021 11:55:12
* Author: Sem
*/
#define F_CPU 10e6
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd_control.h"
void _delay_ms(double __ms);
void lcd_clear() {
lcd_write_command (0x01); //Leeg display
_delay_ms(2);
lcd_write_command (0x80); //Cursor terug naar start
}
void lcd_strobe_lcd_e(void) {
sbi_porta(LCD_E); // E high
_delay_ms(1);
cbi_porta(LCD_E); // E low
_delay_ms(1);
}
void sbi_portc(int index){
PORTC |= (1<<index);
}
void cbi_portc(int index){
PORTC &= ~(1<<index);
}
void sbi_porta(int index){
PORTA |= (1<<index);
}
void cbi_porta(int index){
PORTA &= ~(1<<index);
}
void init_4bits_mode(void) {
// PORTC output mode and all low (also E and RS pin)
// Init I/O
DDRC = 0xFF; // PORTD(7) output, PORTD(6:0) input
PORTC = 0xFF;
DDRA = 0xFF;
PORTC = 0x00;
PORTA = 0x00;
PORTC = 0x20; // function for 4-bit 1 row
lcd_strobe_lcd_e();
PORTC = 0x20; // function high nibble 4-bit 2 row
lcd_strobe_lcd_e();
PORTC = 0x80; // function low nibble 4-bit 2 row
lcd_strobe_lcd_e();
PORTC = 0x00; // function high nibble turn on visible blinking-block cursor
lcd_strobe_lcd_e();
PORTC = 0xF0; // function low nibble turn on visible blinking-block cursor
lcd_strobe_lcd_e();
PORTC = 0x00; // Entry mode set high nibble
lcd_strobe_lcd_e();
PORTC = 0x60; // Entry mode set low nibble
lcd_strobe_lcd_e();
// return home
lcd_write_command(0x02);
lcd_strobe_lcd_e();
}
void lcd_write_character(unsigned char byte){
//upper nibble
PORTC = byte;
sbi_porta(LCD_RS);
lcd_strobe_lcd_e();
//lower nibble
PORTC = (byte<<4);
sbi_porta(LCD_RS);
lcd_strobe_lcd_e();
}
void lcd_write_command(unsigned char byte){
//upper nibble
PORTC = byte;
cbi_porta(LCD_RS);
lcd_strobe_lcd_e();
//lower nibble
PORTC = (byte<<4);
cbi_porta(LCD_RS);
lcd_strobe_lcd_e();
}
void lcd_write_string(const char *str) {
for(;*str; str++){
lcd_write_character(*str);
}
}
void lcd_move_right(void){
lcd_write_command(0x1E);
}
void lcd_write_int(int number)
{
int length = snprintf(NULL, 0, "%d", number);
char str[length + 1];
snprintf(str, length + 1, "%d", number);
lcd_write_string(str);
}

View File

@@ -0,0 +1,30 @@
/*
* lcd_control.h
*
* Created: 24-2-2021 11:56:16
* Author: Sem
*/
#ifndef LCD_CONTROL_H_
#define LCD_CONTROL_H_
#define LCD_E 6
#define LCD_RS 4
void _delay_ms(double __ms);
void lcd_strobe_lcd_e(void);
void sbi_portc(int index);
void cbi_portc(int index);
void sbi_porta(int index);
void cbi_porta(int index);
void init_4bits_mode(void);
void lcd_write_string(const char *str);
void lcd_write_character(unsigned char byte);
void lcd_write_command(unsigned char byte);
void lcd_clear();
void lcd_write_int(int number);
#endif /* LCD_CONTROL_H_ */

View File

@@ -0,0 +1,91 @@
/*
* ultrasonic_sensor.c
*
* Created: 24-3-2021 13:04:52
* Author: Sem
*/
#define F_CPU 10e6
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd_control.h"
static uint16_t timer_dist = 0;
enum interrupt_status {INTERRUPT_FALLING, INTERRUPT_RISING};
static enum interrupt_status int_stat = INTERRUPT_RISING;
void wait_us(unsigned int us)
{
for(int i = 0; i < us; i++)
{
_delay_us(1);
}
}
void wait_ms(unsigned int ms)
{
for(int i = 0; i < ms; i++)
{
_delay_ms(1);
}
}
void ultrasonic_init()
{
DDRG = 0xFF; // port g all output. pin 0 is trig, the rest is for debug
DDRD = 0x00; // port D pin 0 on input. 0 is echo and also interrupt
EICRA = 0x03; // interrupt PORTD on pin 0, rising edge
EIMSK |= 0x01; // enable interrupt on pin 0 (INT0)
TCCR1A = 0b00000000; // initialize timer1, prescaler=256
TCCR1B = 0b00001100; // CTC compare A, RUN
sei(); // turn on interrupt system
}
void ultrasonic_send_pulse()
{
PORTG = 0x00; // 10 us low pulse
wait_us(10);
PORTG = 0x01;
}
void ultrasonic_handle_interrupt()
{
// if the interrupt was generated on a rising edge (start sending echo)
if (int_stat == INTERRUPT_RISING)
{
// set interrupt pin 0 on PORTD to falling edge
EICRA = 0x02;
// reset the time in timer1
TCNT1 = 0x00;
// set interrupt status
int_stat = INTERRUPT_FALLING;
} else
// else if it was generated on a falling edge (end sending echo)
{
// set interrupt pin 0 on PORTD to rising edge
EICRA = 0x03;
// read timer1 into time_dist
timer_dist = TCNT1;
// set interrupt status
int_stat = INTERRUPT_RISING;
}
}
uint16_t ultrasonic_get_timer_dist()
{
return timer_dist;
}

View File

@@ -0,0 +1,12 @@
/*
* ultrasonic_sensor.h
*
* Created: 24-3-2021 13:14:50
* Author: Sem
*/
void ultrasonic_init();
void ultrasonic_send_pulse();
uint16_t ultrasonic_get_timer_dist();
void ultrasonic_handle_interrupt();