/****************************************************************************
LCD-AVR-4d.c - Use an HD44780U based LCD with an Atmel ATmega processor
Copyright (C) 2013 Donald Weiman (weimandn@alfredstate.edu)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/****************************************************************************
File: LCD-AVR-4d.c
Date: September 16, 2013
Target: ATmega328
Compiler: avr-gcc (AVR Studio 6)
Author: Donald Weiman
Summary: 4-bit data interface, busy flag not implemented.
Any LCD pin can be connected to any available I/O port.
Includes a simple write string routine.
*/
/******************************* Program Notes ******************************
This program uses a 4-bit data interface but does not use the
busy flag to determine when the LCD controller is ready. The
LCD RW line (pin 5) is not connected to the uP and it must be
connected to GND for the program to function.
All time delays are longer than those specified in most datasheets
in order to accommodate slower than normal LCD modules. This
requirement is well documented but almost always ignored. The
information is in a note at the bottom of the right hand
(Execution Time) column of the instruction set.
***************************************************************************
The four data lines as well as the two control lines may be
implemented on any available I/O pin of any port. These are
the connections used for this program:
----------- ----------
| ATmega328 | | LCD |
| | | |
| PD7|---------------->|D7 |
| PD6|---------------->|D6 |
| PD5|---------------->|D5 |
| PD4|---------------->|D4 |
| | |D3 |
| | |D2 |
| | |D1 |
| | |D0 |
| | | |
| PB1|---------------->|E |
| | GND --->|RW |
| PB0|---------------->|RS |
----------- ----------
**************************************************************************/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#define endos_copy_port PORTD
#define endos_copy_bit PD6
#define endos_copy_ddr DDRD
#define endos_copy_pin PIND
#define BUTTON_MASK_END_OS (1<<PD6)
#define BUTTON_PIN_END_OS PIND
#define BUTTON_PORT_END_OS PORTD
#define white_upbtn_port PORTC
#define white_upbtn_bit PC1
#define white_upbtn_ddr DDRC
#define white_upbtn_pin PINC
#define BUTTON_MASK_WH_UP (1<<PC1)
#define BUTTON_PIN_WH_UP PINC
#define BUTTON_PORT_WH_UP PORTC
#define BUTTON_MASK_WH_DWN (1<<PC0)
#define BUTTON_PIN_WH_DWN PINC
#define BUTTON_PORT_WH_DWN PORTC
#define white_dwnbtn_port PORTC
#define white_dwnbtn_bit PC0
#define white_dwnbtn_ddr DDRC
#define white_dwnbtn_pin PINC
#define yellow_upbtn_port PORTC
#define yellow_upbtn_bit PC2
#define yellow_upbtn_ddr DDRC
#define yellow_upbtn_pin PINC
#define BUTTON_MASK_YL_UP (1<<PC2)
#define BUTTON_PIN_YL_UP PINC
#define BUTTON_PORT_YL_UP PORTC
#define BUTTON_MASK_YL_DWN (1<<PB5)
#define BUTTON_PIN_YL_DWN PINB
#define BUTTON_PORT_YL_DWN PORTB
#define yellow_dwnbtn_port PORTB
#define yellow_dwnbtn_bit PB5
#define yellow_dwnbtn_ddr DDRB
#define yellow_dwnbtn_pin PINB
#define power_onoffbtn_port PORTD
#define power_onoffbtn_bit PD7
#define power_onoffbtn_ddr DDRD
#define power_onoffbtn_pin PIND
#define BUTTON_MASK_ON_OFF (1<<PD7)
#define BUTTON_PIN_ON_OFF PIND
#define BUTTON_PORT_ON_OFF PORTD
#define power_led_port PORTB
#define power_led_ddr DDRB
#define power_led_pin PINB
#define power_led_bit PB0
#define yellow_led_port PORTB
#define yellow_led_ddr DDRB
#define yellow_led_pin PINB
#define yellow_led_bit PB2
#define white_led_port PORTB
#define white_led_ddr DDRB
#define white_led_pin PINB
#define white_led_bit PB1
#define DEBOUNCE_TIME 25 // time to wait while "de-bouncing" button
#define LOCK_INPUT_TIME 300 // time to wait after a button press
// LCD interface (should agree with the diagram above)
// make sure that the LCD RW pin is connected to GND
#define lcd_D7_port PORTC // lcd D7 connection
#define lcd_D7_bit PC3
#define lcd_D7_ddr DDRC
#define lcd_D6_port PORTC // lcd D6 connection
#define lcd_D6_bit PC4
#define lcd_D6_ddr DDRC
#define lcd_D5_port PORTC // lcd D5 connection
#define lcd_D5_bit PC5
#define lcd_D5_ddr DDRC
#define lcd_D4_port PORTD // lcd D4 connection
#define lcd_D4_bit PD2
#define lcd_D4_ddr DDRD
#define lcd_E_port PORTD // lcd Enable pin
#define lcd_E_bit PD3
#define lcd_E_ddr DDRD
#define lcd_RS_port PORTD // lcd Register Select pin
#define lcd_RS_bit PD4
#define lcd_RS_ddr DDRD
// LCD module information
#define lcd_LineOne 0x00 // start of line 1
#define lcd_LineTwo 0x40 // start of line 2
//#define lcd_LineThree 0x14 // start of line 3 (20x4)
//#define lcd_lineFour 0x54 // start of line 4 (20x4)
//#define lcd_LineThree 0x10 // start of line 3 (16x4)
//#define lcd_lineFour 0x50 // start of line 4 (16x4)
// LCD instructions
#define lcd_Clear 0b00000001 // replace all characters with ASCII 'space'
#define lcd_Home 0b00000010 // return cursor to first position on first line
#define lcd_EntryMode 0b00000110 // shift cursor from left to right on read/write
#define lcd_DisplayOff 0b00001000 // turn display off
#define lcd_DisplayOn 0b00001100 // display on, cursor off, don't blink character
#define lcd_FunctionReset 0b00110000 // reset the LCD
#define lcd_FunctionSet4bit 0b00101000 // 4-bit data, 2-line display, 5 x 7 font
#define lcd_SetCursor 0b10000000 // set cursor position*/
#define lcd_SetCursor_line_1_0 0b10000000 // set cursor position*/
#define lcd_SetCursor_line_1_1 0b10000001 // set cursor position*/
#define lcd_SetCursor_line_1_2 0b10000010 // set cursor position*/
#define lcd_SetCursor_line_1_3 0b10000011 // set cursor position*/
#define lcd_SetCursor_line_1_4 0b10000100 // set cursor position*/
#define lcd_SetCursor_line_1_5 0b10000101 // set cursor position*/
#define lcd_SetCursor_line_1_6 0b10000110 // set cursor position*/
#define lcd_SetCursor_line_1_7 0b10000111 // set cursor position*/
#define lcd_SetCursor_line_1_8 0b10001000 // set cursor position*/
#define lcd_SetCursor_line_1_9 0b10001001 // set cursor position*/
#define lcd_SetCursor_line_1_10 0b10001010 // set cursor position*/
#define lcd_SetCursor_line_1_11 0b10001011 // set cursor position*/
#define lcd_SetCursor_line_1_12 0b10001100 // set cursor position*/
#define lcd_SetCursor_line_1_13 0b10001101 // set cursor position*/
#define lcd_SetCursor_line_1_14 0b10001110 // set cursor position*/
#define lcd_SetCursor_line_1_15 0b10001111 // set cursor position*/
#define lcd_SetCursor_line_2_0 0xC0 // set cursor position*/
#define lcd_SetCursor_line_2_1 0xC1 // set cursor position*/
#define lcd_SetCursor_line_2_2 0xC2 // set cursor position*/
#define lcd_SetCursor_line_2_3 0xC3 // set cursor position*/
#define lcd_SetCursor_line_2_4 0xC4 // set cursor position*/
#define lcd_SetCursor_line_2_5 0xC5 // set cursor position*/
#define lcd_SetCursor_line_2_6 0xC6 // set cursor position*/
#define lcd_SetCursor_line_2_7 0xC7 // set cursor position*/
#define lcd_SetCursor_line_2_8 0xC8 // set cursor position*/
#define lcd_SetCursor_line_2_9 0xC9 // set cursor position*/
#define lcd_SetCursor_line_2_10 0xCA // set cursor position*/
#define lcd_SetCursor_line_2_11 0xCB // set cursor position*/
#define lcd_SetCursor_line_2_12 0xCC // set cursor position*/
#define lcd_SetCursor_line_2_13 0xCD // set cursor position*/
#define lcd_SetCursor_line_2_14 0xCE // set cursor position*/
#define lcd_SetCursor_line_2_15 0xCF // set cursor position*/
#define atoa(x) #x
// Program ID
//uint8_t program_author[] = atoa(32);//Contstant data convert to string not variable
uint8_t program_author[] = " Ranojan";
uint8_t WHITE[] = " White:";
uint8_t YELLOW[] = " Yellow:";
uint8_t PERCENTAGE[] = " %";
uint8_t ENDOSCOPY_MODE[] = "ENDOSCOPY MODE";
uint8_t ACTIVE[] = "ACTIVE";
uint8_t SPACE[] = " ";
uint8_t LED_OT_LIGHT[] = "LED OT LIGHT";
uint8_t CONTROLLER[] = "CONTROLLER";
//uint8_t program_version[] = "LCD-AVR(gcc)";
uint8_t program_version[] = "LCD-AVR(gcc)";
uint8_t program_date[] = "Sep 16, 2013";
uint8_t maxBrightness = 255; //this can be any number - it's the number of steps between dimmest and brightest.
uint8_t digits[7];
// variables will change:
uint8_t brightness = 0;
uint8_t interval[] = "25";
unsigned int addr_white = 0;
unsigned int addr_yellow = 1;
unsigned int addr_yellow_up = 2;
unsigned int addr_yellow_dwn = 3;
uint8_t rb_wh = 'a';
uint8_t rb_yl = 'b';
// Function Prototypes
void lcd_write_4(uint8_t);
void lcd_write_instruction_4d(uint8_t);
void lcd_write_character_4d(uint8_t);
void lcd_write_string_4d(uint8_t *);
void lcd_init_4d(void);
// Variable to tell main that the button is pressed (and debounced).
// Main will clear it after a detected button press.
volatile uint8_t button_wh_down;
volatile uint8_t button_wh_up;
volatile uint8_t button_yl_up;
volatile uint8_t button_yl_down;
volatile uint8_t button_endos;
volatile uint8_t button_on_off;
unsigned int pwm = 14;//pwm = 0x0;
unsigned int pwmb = 14;
bool up = true;
//OCR1A = 0;
unsigned char n_led = 1; // LED number is on now
unsigned char state_bt = 0;
unsigned char state_bt_power = 0;
unsigned char power_led = 1;
/******************************* Main Program Code *************************/
int main(void)
{
uint8_t brightness = 0;
//InitPWM();
//initTimer();
//DDRB |= (1 << PB1) | (1 << PB2);
/************************************************************************
Endoscopy Push Button
************************************************************************/
// Enable internal pullup resistor on the input pin
BUTTON_PORT_WH_UP |= BUTTON_MASK_WH_UP;
BUTTON_PORT_WH_DWN |= BUTTON_MASK_WH_DWN;
BUTTON_PORT_YL_UP |= BUTTON_MASK_YL_UP;
BUTTON_PORT_YL_DWN |= BUTTON_MASK_YL_DWN;
BUTTON_PORT_ON_OFF |= BUTTON_MASK_ON_OFF;
BUTTON_PORT_END_OS |= BUTTON_MASK_END_OS;
//yellow_led_ddr |= (1 << yellow_led_bit);
//white_led_ddr |= (1 << white_led_bit);
// configure the microprocessor pins for the data lines
lcd_D7_ddr |= (1 << lcd_D7_bit); // 4 data lines - output
lcd_D6_ddr |= (1 << lcd_D6_bit);
lcd_D5_ddr |= (1 << lcd_D5_bit);
lcd_D4_ddr |= (1 << lcd_D4_bit);
// configure the microprocessor pins for the control lines
lcd_E_ddr |= (1 << lcd_E_bit); // E line - output
lcd_RS_ddr |= (1 << lcd_RS_bit); // RS line - output
// initialize the LCD controller as determined by the defines (LCD instructions)
lcd_init_4d(); // initialize the LCD display for a 4-bit interface
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineOne);
// display the first line of information
lcd_write_string_4d(program_author);
// set cursor to start of second line
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineTwo);
_delay_us(80); // 40 uS delay (min)
// display the second line of information
lcd_write_string_4d(program_version);
/**
We will be using OCR1A as our PWM output which is the
same pin as PB1.
*/
DDRB |= _BV(PB1);
DDRB |= _BV(PB2);
power_led_ddr |= (1 << power_led_bit); // Power LED as output
ledOTLightInitDisplay();
/**
There are quite a number of PWM modes available but for the
sake of simplicity we'll just use the 8-bit Fast PWM mode.
This is done by setting the WGM10 and WGM12 bits. We
Setting COM1A1 tells the microcontroller to set the
output of the OCR1A pin low when the timer's counter reaches
a compare value (which will be explained below). CS10 being
set simply turns the timer on without a prescaler (so at full
speed). The timer is used to determine when the PWM pin should be
on and when it should be off.
*/
TCCR1A |= _BV(COM1A1) | _BV(WGM10) | (1 << COM1B1);
TCCR1B |= _BV(CS10) | _BV(WGM12);
//TCCR1A=(1<<COM1B1)|(1<<COM1B0)|(1<<WGM11); //Set OC1A/OC1B on Compare Match, clear OC1A/OC1B at BOTTOM (inverting mode)
//TCCR1B=(1<<WGM13)|(1<<WGM12)|(1<<CS11); //set to Fast PWM TOP is ICR1/ set prescale to 8
//ICR1=250;
//TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0) | (1<<WGM11) | (0<<WGM10);
//TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
/**
This loop is used to change the value in the OCR1A register.
What that means is we're telling the timer waveform generator
the point when it should change the state of the PWM pin.
The way we configured it (with _BV(COM1A1) above) tells the
generator to have the pin be on when the timer is at zero and then
to turn it off once it reaches the value in the OCR1A register.
Given that we are using an 8-bit mode the timer will reset to zero
after it reaches 0xff, so we have 255 ticks of the timer until it
resets. The value stored in OCR1A is the point within those 255
ticks of the timer when the output pin should be turned off
(remember, it starts on).
Effectively this means that the ratio of pwm / 255 is the percentage
of time that the pin will be high. Given this it isn't too hard
to see what when the pwm value is at 0x00 the LED will be off
and when it is 0xff the LED will be at its brightest.
*/
// *******************************************************************************************************************/
power_led_port |= (1<<power_led_bit);
// endless loop
while (1)
{
//debounce_power_on();
debounce_endoscopy();
debounce_yello_up();
debounce_white_up();
debounce_white_down();
debounce_yellow_down();
/*OCR1A = pwm;
pwm += up ? 1 : -1;
if (pwm == 0xff)
up = false;
else if (pwm == 0x00)
up = true;
_delay_ms(10);*/
if(button_state_power_on_off())
{
//power_led_port |= (1<<power_led_bit);
//_delay_ms(2000);
//power_led_port &= ~(1<<power_led_bit);
//_delay_ms(2000);
switch(power_led){
case 1:
power_led_port &= ~(1<<power_led_bit);
//PORTC ^= (1<<LED2); // toggling the current state of the pin LED2. LED2 is turn off.
//PORTD ^= (1<<LED3); // toggling the current state of the pin LED3. LED3 is turn on.
state_bt_power = 1;
state_bt = 1;
readEEPROMData();
initEEPROMData();
break;
case 2:
power_led_port |= (1<<power_led_bit);
ledOTLightInitDisplay();
OCR1A = 0x0;
OCR1B = 0x0;
//PORTB ^= (1<<LED1); // toggling the current state of the pin LED1. LED1 is turn off.
//PORTC ^= (1<<LED2); // toggling the current state of the pin LED2. LED2 is turn on.
state_bt_power = 0;
power_led=0; // reset LED number
state_bt = 0;
break;
}
power_led++; // next LED is turn on
_delay_ms(LOCK_INPUT_TIME);
}
if (!state_bt_power|!state_bt){
button_wh_up = 0;
button_wh_down = 0;
button_yl_up = 0;
button_yl_down = 0;
}
if(state_bt_power){
if(state_bt){
// Check if the button is pressed.
if (button_wh_up)
{
// Clear flag
button_wh_up = 0;
pwm += 1;
OCR1A = pwm;
// Toggle the LED
//white_led_port ^= (1<<white_led_bit);
//SetPWMOutput(brightness);
writePercentageData(pwm,pwmb);
Wait();
}
// Check if the button is pressed.
if (button_wh_down)
{
//Clear flag
button_wh_down = 0;
//Toggle the LED
//white_led_port ^= (1<<white_led_bit);
pwm -= 1;
OCR1A = pwm;
//SetPWMOutput(brightness);
writePercentageData(pwm,pwmb);
Wait();
}
// Check if the button is pressed.
if (button_yl_up)
{
//Clear flag
button_yl_up = 0;
//Toggle the LED
pwmb += 1;
OCR1B = pwmb;
//white_led_port ^= (1<<white_led_bit);
//yellow_led_port ^= (1<<yellow_led_bit);
//SetPWMOutput(brightness);
writePercentageData(pwm,pwmb);
Wait();
}
//Check if the button is pressed.
if (button_yl_down)
{
//Clear flag
button_yl_down = 0;
//Toggle the LED
//yellow_led_port ^= (1<<yellow_led_bit);
pwmb -= 1;
OCR1B = pwmb;
writePercentageData(pwm,pwmb);
//SetPWMOutput(brightness);
Wait();
}
}
}
/*else{
BUTTON_PORT_WH_UP &= ~(BUTTON_MASK_WH_UP);
BUTTON_PORT_WH_DWN &= ~(BUTTON_MASK_WH_DWN);
BUTTON_PORT_YL_UP &= ~(BUTTON_MASK_YL_UP);
BUTTON_PORT_YL_DWN &= (BUTTON_MASK_YL_DWN);
BUTTON_PORT_ON_OFF |= (BUTTON_MASK_ON_OFF);
BUTTON_PORT_END_OS |= (BUTTON_MASK_END_OS);
pwm = 0xE;
pwmb = 0xE;
OCR1A = 0;
OCR1B = 0;
}*/
/*if (white_upbtn_state()) {
SetPWMOutput(brightness);
brightness= brightness+25;
Wait();
}*/
/* if (white_dwnbtn_state()) {
brightness= brightness-25;
SetPWMOutput(brightness);
Wait();
}*/
/* for(brightness=0;brightness<255;brightness++)
{
SetPWMOutput(brightness);
Wait();
}*/
/*if((white_upbtn_pin & 1<< white_upbtn_bit) ){
brightness = brightness ;
_delay_ms(1000);
lcd_init_4d(); // initialize the LCD display for a 4-bit interface
// set cursor to start of second line
//lcd_write_instruction_4d(lcd_SetCursor | lcd_LineTwo);
_delay_ms(1000); // 40 uS delay (min)
// display the second line of information
lcd_write_string_4d(interval);
//lcd_write_character_4d(0b010100);
_delay_ms(1000);
}*/
/*if(button_state_endoscopy())
{
//power_led_port |= (1<<power_led_bit);
//_delay_ms(2000);
//power_led_port &= ~(1<<power_led_bit);
//_delay_ms(2000);
switch(n_led){
case 1:
power_led_port |= (1<<power_led_bit);
//PORTB ^= (1<<LED1); // toggling the current state of the pin LED1. LED1 is turn off.
//PORTC ^= (1<<LED2); // toggling the current state of the pin LED2. LED2 is turn on.
state_bt = 0;
break;
case 2:
power_led_port &= ~(1<<power_led_bit);
//PORTC ^= (1<<LED2); // toggling the current state of the pin LED2. LED2 is turn off.
//PORTD ^= (1<<LED3); // toggling the current state of the pin LED3. LED3 is turn on.
n_led=0; // reset LED number
state_bt = 1;
break;
}
n_led++; // next LED is turn on
_delay_ms(LOCK_INPUT_TIME);
}*/
//rb8 = eeprom_read_byte((uint8_t*)addr)
if(state_bt_power){
if (button_state_endoscopy()) {
switch(n_led){
case 1:
/*************************************************************************
Initial EEPROM DATA
**************************************************************************/
initEEPROMData();
/*******************************************************************************************************************
Write to eeprom
*******************************************************************************************************************/
eeprom_write_byte((uint8_t*)addr_white, pwm );
eeprom_write_byte((uint8_t*)addr_yellow, pwmb );
//eeprom_write_byte((uint8_t*)addr_yellow_up, wb_yellow_up );
//eeprom_write_byte((uint8_t*)addr_yellow_dwn, wb_yellow_dwn );
//******************************************************************************************************************
readEEPROMData();
state_bt = 1;
break;
case 2:
lcd_init_4d();
lcd_write_instruction_4d(lcd_SetCursor_line_1_1);
lcd_write_string_4d(SPACE);
lcd_write_string_4d(ENDOSCOPY_MODE);
lcd_write_instruction_4d(lcd_SetCursor_line_2_5 | lcd_LineTwo);
lcd_write_string_4d(SPACE);
lcd_write_string_4d(ACTIVE);
//writePercentageData(24, 24);
eeprom_write_byte((uint8_t*)addr_white, pwm );
eeprom_write_byte((uint8_t*)addr_yellow, pwmb );
OCR1A = 24;
OCR1B = 24;
n_led=0; // reset LED number
state_bt = 0;
break;
}
n_led++; // next LED is turn on
_delay_ms(LOCK_INPUT_TIME);
}
}
}
return 0;
}
void ledOTLightInitDisplay(){
lcd_init_4d();
lcd_write_instruction_4d(lcd_SetCursor_line_1_2);
lcd_write_string_4d(SPACE);
lcd_write_string_4d(LED_OT_LIGHT);
lcd_write_instruction_4d(lcd_SetCursor_line_2_3 | lcd_LineTwo);
lcd_write_string_4d(SPACE);
lcd_write_string_4d(CONTROLLER);
}
void writePercentageData(unsigned int pwm, unsigned int pwmb)
{
lcd_init_4d();
uint8_t PWM[6];
uint8_t apa[6] = " ";
uint8_t YL_PWM[6];
tostring(YL_PWM, pwmb);
tostring(PWM, pwm);
lcd_write_instruction_4d(lcd_SetCursor_line_1_0);
lcd_write_string_4d(WHITE);
//_delay_ms(1000);
lcd_write_instruction_4d(lcd_SetCursor_line_1_8);
lcd_write_string_4d(apa);
lcd_write_string_4d(PWM);
lcd_write_instruction_4d(lcd_SetCursor_line_1_11);
lcd_write_string_4d(PERCENTAGE);
lcd_write_instruction_4d(lcd_SetCursor_line_2_0 | lcd_LineTwo);
// display the second line of information
lcd_write_string_4d(YELLOW);
lcd_write_instruction_4d(lcd_SetCursor_line_2_8);
// display the second line of information
lcd_write_string_4d(apa);
lcd_write_string_4d(YL_PWM);
lcd_write_instruction_4d(lcd_SetCursor_line_2_11);
lcd_write_string_4d(PERCENTAGE);
//_delay_ms(1000);
}
void writeYellowData(unsigned int pwmb)
{
lcd_init_4d();
uint8_t YL_PWM[6];
uint8_t apa[6] = " ";
tostring(YL_PWM, pwmb);
lcd_write_instruction_4d(lcd_SetCursor_line_2_0 | lcd_LineTwo);
// display the second line of information
lcd_write_string_4d(YELLOW);
lcd_write_instruction_4d(lcd_SetCursor_line_2_8);
// display the second line of information
lcd_write_string_4d(apa);
lcd_write_string_4d(YL_PWM);
lcd_write_instruction_4d(lcd_SetCursor_line_2_11);
lcd_write_string_4d(PERCENTAGE);
_delay_ms(1000);
}
void initEEPROMData()
{
pwm = eeprom_read_byte((uint8_t*)addr_white);
pwmb = eeprom_read_byte((uint8_t*)addr_yellow);
OCR1A = pwm;
OCR1B = pwmb;
}
void readEEPROMData()
{
rb_wh = eeprom_read_byte((uint8_t*)addr_white);
rb_yl = eeprom_read_byte((uint8_t*)addr_yellow);
//rb_yellow_up = eeprom_read_byte((uint8_t*)addr_yellow_up);
//rb_yellow_dwn = eeprom_read_byte((uint8_t*)addr_yellow_dwn);
lcd_init_4d();
uint8_t WH_PWM[6];
uint8_t YL_PWM[6];
uint8_t apa[6] = " ";
tostring(WH_PWM, rb_wh);
//concatenate_string(WHITE, WH_PWM);
//strcat((char*)WHITE, (char*)WH_PWM);
tostring(YL_PWM, rb_yl);
//concatenate_string(YELLOW, YL_PWM);
lcd_write_instruction_4d(lcd_SetCursor_line_1_0);
lcd_write_string_4d(WHITE);
//_delay_ms(1000);
lcd_write_instruction_4d(lcd_SetCursor_line_1_8);
// display the second line of information
//moveSpaceInFront((char*)WH_PWM);
//add_spaces((char*)WH_PWM,1);
//lcd_write_instruction_4d(lcd_SetCursor_line_1_8);
lcd_write_string_4d(apa);
lcd_write_string_4d(WH_PWM);
lcd_write_instruction_4d(lcd_SetCursor_line_2_0 | lcd_LineTwo);
// display the second line of information
lcd_write_string_4d(YELLOW);
lcd_write_instruction_4d(lcd_SetCursor_line_2_8);
// display the second line of information
lcd_write_string_4d(apa);
lcd_write_string_4d(YL_PWM);
lcd_write_instruction_4d(lcd_SetCursor_line_1_11);
lcd_write_string_4d(PERCENTAGE);
lcd_write_instruction_4d(lcd_SetCursor_line_2_11);
lcd_write_string_4d(PERCENTAGE);
_delay_ms(1000);
}
// Function to find spaces and move to beginning
void moveSpaceInFront(char str[])
{
// Keep copying non-space characters
int i = strlen(str);
for (int j=i; j >= 0; j--)
if (str[j] != ' ')
str[i--] = str[j];
// Move spaces to be beginning
while (i >= 0)
str[i--] = ' ';
}
void add_spaces(char *dest, int num_of_spaces) {
int i;
int len = strlen(dest);
for (i = 0 ; i < num_of_spaces ; i++) {
dest[len + i] = ' ';
}
dest[len + num_of_spaces] = '\0';
}
void tostring(uint8_t str[], int num)
{
int i, rem, len = 0, n;
n = num;
while (n != 0)
{
len++;
n /= 10;
}
for (i = 0; i < len; i++)
{
rem = num % 10;
num = num / 10;
str[len - (i + 1)] = rem + '0';
}
str[len] = '\0';
}
void concatenate_string(uint8_t *original, uint8_t *add)
{
while (*original)
original++;
while (*add)
{
*original = *add;
add++;
original++;
}
*original = '\0';
//*add = '\0';
}
void SetPWMOutput(uint8_t duty)
{
OCR1A = duty;
}
void Wait()
{
_delay_ms(10);
}
unsigned char button_state_power_on_off()
{
/* the button is pressed when BUTTON1 bit is clear */
if (!(power_onoffbtn_pin & (1 << power_onoffbtn_bit)))
{
_delay_ms(DEBOUNCE_TIME);
if (!(power_onoffbtn_pin & (1 << power_onoffbtn_bit))) return 1;
}
return 0;
}
//white_dwnbtn_pin
unsigned char white_upbtn_state()
{
/* the button is pressed when BUTTON1 bit is clear */
if (!(white_upbtn_pin & 1 << white_upbtn_bit))
{
_delay_ms(DEBOUNCE_TIME);
if (!(white_upbtn_pin & 1 << white_upbtn_bit)) return 1;
}
return 0;
}
unsigned char white_dwnbtn_state()
{
/* the button is pressed when BUTTON1 bit is clear */
if (!(white_dwnbtn_pin & 1 << white_dwnbtn_bit))
{
_delay_ms(DEBOUNCE_TIME);
if (!(white_dwnbtn_pin & 1 << white_dwnbtn_bit)) return 1;
}
return 0;
}
unsigned char button_state_endoscopy()
{
/* the button is pressed when BUTTON1 bit is clear */
if (!(endos_copy_pin & (1 << endos_copy_bit)))
{
_delay_ms(DEBOUNCE_TIME);
if (!(endos_copy_pin & (1 << endos_copy_bit))) return 1;
}
return 0;
}
void initTimer() {
ICR1 = 40000;
//OCR1A = n * x;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
}
/******************************* End of Main Program Code ******************/
/*============================== 4-bit LCD Functions ======================*/
/*
Name: lcd_init_4d
Entry: equates (LCD instructions) set up for the desired operation
Exit: no parameters
Notes: uses time delays rather than checking the busy flag
*/
void lcd_init_4d(void)
{
// Power-up delay
_delay_ms(100); // initial 40 mSec delay
// IMPORTANT - At this point the LCD module is in the 8-bit mode and it is expecting to receive
// 8 bits of data, one bit on each of its 8 data lines, each time the 'E' line is pulsed.
//
// Since the LCD module is wired for the 4-bit mode, only the upper four data lines are connected to
// the microprocessor and the lower four data lines are typically left open. Therefore, when
// the 'E' line is pulsed, the LCD controller will read whatever data has been set up on the upper
// four data lines and the lower four data lines will be high (due to internal pull-up circuitry).
//
// Fortunately the 'FunctionReset' instruction does not care about what is on the lower four bits so
// this instruction can be sent on just the four available data lines and it will be interpreted
// properly by the LCD controller. The 'lcd_write_4' subroutine will accomplish this if the
// control lines have previously been configured properly.
// Set up the RS and E lines for the 'lcd_write_4' subroutine.
lcd_RS_port &= ~(1 << lcd_RS_bit); // select the Instruction Register (RS low)
lcd_E_port &= ~(1 << lcd_E_bit); // make sure E is initially low
// Reset the LCD controller
lcd_write_4(lcd_FunctionReset); // first part of reset sequence
_delay_ms(10); // 4.1 mS delay (min)
lcd_write_4(lcd_FunctionReset); // second part of reset sequence
_delay_us(200); // 100uS delay (min)
lcd_write_4(lcd_FunctionReset); // third part of reset sequence
_delay_us(200); // this delay is omitted in the data sheet
// Preliminary Function Set instruction - used only to set the 4-bit mode.
// The number of lines or the font cannot be set at this time since the controller is still in the
// 8-bit mode, but the data transfer mode can be changed since this parameter is determined by one
// of the upper four bits of the instruction.
lcd_write_4(lcd_FunctionSet4bit); // set 4-bit mode
_delay_us(80); // 40uS delay (min)
// Function Set instruction
lcd_write_instruction_4d(lcd_FunctionSet4bit); // set mode, lines, and font
_delay_us(80); // 40uS delay (min)
// The next three instructions are specified in the data sheet as part of the initialization routine,
// so it is a good idea (but probably not necessary) to do them just as specified and then redo them
// later if the application requires a different configuration.
// Display On/Off Control instruction
lcd_write_instruction_4d(lcd_DisplayOff); // turn display OFF
_delay_us(80); // 40uS delay (min)
// Clear Display instruction
lcd_write_instruction_4d(lcd_Clear); // clear display RAM
_delay_ms(4); // 1.64 mS delay (min)
// ; Entry Mode Set instruction
lcd_write_instruction_4d(lcd_EntryMode); // set desired shift characteristics
_delay_us(80); // 40uS delay (min)
// This is the end of the LCD controller initialization as specified in the data sheet, but the display
// has been left in the OFF condition. This is a good time to turn the display back ON.
// Display On/Off Control instruction
lcd_write_instruction_4d(lcd_DisplayOn); // turn the display ON
_delay_us(80); // 40uS delay (min)
}
/*...........................................................................
Name: lcd_write_string_4d
; Purpose: display a string of characters on the LCD
Entry: (theString) is the string to be displayed
Exit: no parameters
Notes: uses time delays rather than checking the busy flag
*/
void lcd_write_string_4d(uint8_t theString[])
{
volatile int i = 0; // character counter*/
while (theString[i] != 0)
{
lcd_write_character_4d(theString[i]);
i++;
_delay_us(80); // 40 uS delay (min)
}
}
/*...........................................................................
Name: lcd_write_character_4d
Purpose: send a byte of information to the LCD data register
Entry: (theData) is the information to be sent to the data register
Exit: no parameters
Notes: does not deal with RW (busy flag is not implemented)
*/
void lcd_write_character_4d(uint8_t theData)
{
lcd_RS_port |= (1 << lcd_RS_bit); // select the Data Register (RS high)
lcd_E_port &= ~(1 << lcd_E_bit); // make sure E is initially low
lcd_write_4(theData); // write the upper 4-bits of the data
lcd_write_4(theData << 4); // write the lower 4-bits of the data
}
/*...........................................................................
Name: lcd_write_instruction_4d
Purpose: send a byte of information to the LCD instruction register
Entry: (theInstruction) is the information to be sent to the instruction register
Exit: no parameters
Notes: does not deal with RW (busy flag is not implemented)
*/
void lcd_write_instruction_4d(uint8_t theInstruction)
{
lcd_RS_port &= ~(1 << lcd_RS_bit); // select the Instruction Register (RS low)
lcd_E_port &= ~(1 << lcd_E_bit); // make sure E is initially low
lcd_write_4(theInstruction); // write the upper 4-bits of the data
lcd_write_4(theInstruction << 4); // write the lower 4-bits of the data
}
/*...........................................................................
Name: lcd_write_4
Purpose: send a byte of information to the LCD module
Entry: (theByte) is the information to be sent to the desired LCD register
RS is configured for the desired LCD register
E is low
RW is low
Exit: no parameters
Notes: use either time delays or the busy flag
*/
void lcd_write_4(uint8_t theByte)
{
lcd_D7_port &= ~(1 << lcd_D7_bit); // assume that data is '0'
if (theByte & 1 << 7) lcd_D7_port |= (1 << lcd_D7_bit); // make data = '1' if necessary
lcd_D6_port &= ~(1 << lcd_D6_bit); // repeat for each data bit
if (theByte & 1 << 6) lcd_D6_port |= (1 << lcd_D6_bit);
lcd_D5_port &= ~(1 << lcd_D5_bit);
if (theByte & 1 << 5) lcd_D5_port |= (1 << lcd_D5_bit);
lcd_D4_port &= ~(1 << lcd_D4_bit);
if (theByte & 1 << 4) lcd_D4_port |= (1 << lcd_D4_bit);
// write the data
// Address set-up time' (40 nS)
lcd_E_port |= (1 << lcd_E_bit); // Enable pin high
_delay_us(1); // implement 'Data set-up time' (80 nS) and 'Enable pulse width' (230 nS)
lcd_E_port &= ~(1 << lcd_E_bit); // Enable pin low
_delay_us(1); // implement 'Data hold time' (10 nS) and 'Enable cycle time' (500 nS)
}
// Check button state and set the button_down variable if a debounced
// button down press is detected.
// Call this function about 100 times per second.
static inline void debounce_white_down(void)
{
// Counter for number of equal states
static uint8_t count = 0;
// Keeps track of current (debounced) state
static uint8_t button_state = 0;
// Check if button is high or low for the moment
uint8_t current_state = (~BUTTON_PIN_WH_DWN & BUTTON_MASK_WH_DWN) != 0;
if (current_state != button_state) {
// Button state is about to be changed, increase counter
count++;
if (count >= 4) {
// The button have not bounced for four checks, change state
button_state = current_state;
// If the button was pressed (not released), tell main so
if (current_state != 0) {
button_wh_down = 1;
}
count = 0;
}
} else {
// Reset counter
count = 0;
}
}
// Check button state and set the button_down variable if a debounced
// button down press is detected.
// Call this function about 100 times per second.
static inline void debounce_white_up(void)
{
// Counter for number of equal states
static uint8_t count = 0;
// Keeps track of current (debounced) state
static uint8_t button_state = 0;
// Check if button is high or low for the moment
uint8_t current_state = (~BUTTON_PIN_WH_UP & BUTTON_MASK_WH_UP) != 0;
if (current_state != button_state) {
// Button state is about to be changed, increase counter
count++;
if (count >= 4) {
// The button have not bounced for four checks, change state
button_state = current_state;
// If the button was pressed (not released), tell main so
if (current_state != 0) {
button_wh_up = 1;
}
count = 0;
}
} else {
// Reset counter
count = 0;
}
}
// Check button state and set the button_down variable if a debounced
// button down press is detected.
// Call this function about 100 times per second.
static inline void debounce_yellow_down(void)
{
// Counter for number of equal states
static uint8_t count = 0;
// Keeps track of current (debounced) state
static uint8_t button_state = 0;
// Check if button is high or low for the moment
uint8_t current_state = (~BUTTON_PIN_YL_DWN & BUTTON_MASK_YL_DWN) != 0;
if (current_state != button_state) {
// Button state is about to be changed, increase counter
count++;
if (count >= 4) {
// The button have not bounced for four checks, change state
button_state = current_state;
// If the button was pressed (not released), tell main so
if (current_state != 0) {
button_yl_down = 1;
}
count = 0;
}
} else {
// Reset counter
count = 0;
}
}
// Check button state and set the button_down variable if a debounced
// button down press is detected.
// Call this function about 100 times per second.
static inline void debounce_yello_up(void)
{
// Counter for number of equal states
static uint8_t count = 0;
// Keeps track of current (debounced) state
static uint8_t button_state = 0;
// Check if button is high or low for the moment
uint8_t current_state = (~BUTTON_PIN_YL_UP & BUTTON_MASK_YL_UP) != 0;
if (current_state != button_state) {
// Button state is about to be changed, increase counter
count++;
if (count >= 4) {
// The button have not bounced for four checks, change state
button_state = current_state;
// If the button was pressed (not released), tell main so
if (current_state != 0) {
button_yl_up = 1;
}
count = 0;
}
} else {
// Reset counter
count = 0;
}
}
// Check button state and set the button_down variable if a debounced
// button down press is detected.
// Call this function about 100 times per second.
static inline void debounce_endoscopy(void)
{
// Counter for number of equal states
static uint8_t count = 0;
// Keeps track of current (debounced) state
static uint8_t button_state = 0;
// Check if button is high or low for the moment
uint8_t current_state = (~BUTTON_PIN_END_OS & BUTTON_MASK_END_OS) != 0;
if (current_state != button_state) {
// Button state is about to be changed, increase counter
count++;
if (count >= 4) {
// The button have not bounced for four checks, change state
button_state = current_state;
// If the button was pressed (not released), tell main so
if (current_state != 0) {
button_endos = 1;
}
count = 0;
}
} else {
// Reset counter
count = 0;
}
}
// Check button state and set the button_down variable if a debounced
// button down press is detected.
// Call this function about 100 times per second.
static inline void debounce_power_on(void)
{
// Counter for number of equal states
static uint8_t count = 0;
// Keeps track of current (debounced) state
static uint8_t button_state = 0;
// Check if button is high or low for the moment
uint8_t current_state = (~BUTTON_PIN_ON_OFF & BUTTON_MASK_ON_OFF) != 0;
if (current_state != button_state) {
// Button state is about to be changed, increase counter
count++;
if (count >= 4) {
// The button have not bounced for four checks, change state
button_state = current_state;
// If the button was pressed (not released), tell main so
if (current_state != 0) {
button_on_off = 1;
}
count = 0;
}
} else {
// Reset counter
count = 0;
}
}
Â
Comments50
propecia online prescription
dyinrsgf
vvylmqqz
evjetpta
lkmklfcm
yhmuqeat
qeuqlylq
ljyoimpa
hmfyqhlj
zivwkypbjc
kmfathyu
ourdzosqno
kwkfwirm
ytolorjv
vqbzceub
zgvaqsct
xhoqpcpz
xlrbvnnmjy
tpyhiqse
lggcnhgjc
zfetvalx
dfpijkbt
vhighqgs
elpibmbx
katkbjiq
cgaxbnfm
hebubxxi
rdgbhnxf
vxzdnmtk
ehxmoqtdy
mkdlvhls
vurorkec
xuehjtjq
nxhupjaix
ecwysods
fghktxdk
hzmjfuoq
tlmbjydg
yseqyaeb
wgmhsjwx
lgztdxlxy
bpjkjphc
ktixghjv
uzdlirte
giictdew
oayzwobi
akkrterp
lceuudja
npiawoye
abyjnyst