BASIC4MCU | 질문게시판 | 답변 : 아래 ATMEGA LCD 출력 코드 및 에러 확인 질문 이미지 첨부합니다.
페이지 정보
작성자 master 작성일2022-06-03 15:27 조회1,251회 댓글0건본문
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define F_CPU 16000000UL
#define LCD_RS PORTC4
#define LCD_EN PORTC5
#define LCD_PORT PORTC
#define LCD_Clear 0x01
#define LCD_Cursor 0x03
#define LCD_Function_Set 0x28
#define LCD_On 0x0E
#define LCD_Entry 0x06
volatile float calibrationfactor = 4.5;
volatile float flowrate=0;
volatile float cal=0;
volatile int pulsecount=0;
volatile int millisec=0;
volatile char sec=0;
void init_port(){
DDRD = 0xFC;
PORTD = 0x44;
}
void init_timer1(){
TCCR1A = (0<<WGM10)|(0<<COM1A0);
TCCR1B = (1<<WGM12)|(0<<CS12)|(1<<CS11)|(1<<CS10);
OCR1A = 249;;
TIMSK = (1<<OCIE1A);
/*TCCR0A = (1 << WGM01);
TIMSK0 |= (1 << OCIE0A);
TCCR0B |= (1 << CS02) | (1 << CS00);
OCR0A = 0x07;*/
}
// LCD
void Write_Command_LCD(char Command){
LCD_PORT = (LCD_PORT & 0x0F) | (Command & 0xF0); //상위 4BIT 출력
//LCD_PORT=Command; <- 8bit를 사용할 경
LCD_PORT &= ~(1<<LCD_RS); // RS=0, command reg.
LCD_PORT |= (1<<LCD_EN); //LCD_E=1; //하강 Edge에서 데이터를 전달하기 때문에 High 상태 유지 후
_delay_ms(1);
LCD_PORT &= ~(1<<LCD_EN); //LCD_E=0; //low 상태로 전환하여 데이터를 전달.
_delay_ms(1);
LCD_PORT = (LCD_PORT & 0x0F) | (Command << 4); //하위 4BIT 출력
LCD_PORT |= (1<<LCD_EN); //LCD_E=1; //하강 Edge에서 데이터를 전달하기 때문에 High 상태 유지 후
_delay_ms(1);
LCD_PORT &= ~(1<<LCD_EN); //LCD_E=0; //low 상태로 전환하여 데이터를 전달.
_delay_ms(10);
}
void PutsChar_LCD(int data){
LCD_PORT = (LCD_PORT & 0x0F) | (data & 0xF0); //상위 4BIT 출력
LCD_PORT &= ~(1<<LCD_RS); // RS=0, command reg.
LCD_PORT |= (1<<LCD_EN); //LCD_E=1; //하강 Edge에서 데이터를 전달하기 때문에 High 상태 유지 후
_delay_ms(1);
LCD_PORT &= ~(1<<LCD_EN); //LCD_E=0; //low 상태로 전환하여 데이터를 전달.
_delay_ms(1);
LCD_PORT = (LCD_PORT & 0x0F) | (data << 4); //하위 4BIT 출력
LCD_PORT |= (1<<LCD_EN); //LCD_E=1; //하강 Edge에서 데이터를 전달하기 때문에 High 상태 유지 후
_delay_ms(1);
LCD_PORT &= ~(1<<LCD_EN); //LCD_E=0; //low 상태로 전환하여 데이터를 전달.
_delay_ms(10);
}
void String_LCD(char *s){ while(*s)PutsChar_LCD(*s++); }
//
void LCD_Init(void){
_delay_ms(50);
Write_Command_LCD(LCD_Function_Set); _delay_ms(2);
Write_Command_LCD(LCD_On); _delay_ms(2);
Write_Command_LCD(LCD_Clear); _delay_ms(2);
Write_Command_LCD(LCD_Entry); _delay_ms(2);
Write_Command_LCD(0x02); _delay_ms(2); //Return Home
}
//
ISR (TIMER1_COMPA_vect)
{
if(++millisec>=1000)
{ millisec=0; // 1sec
if(sec<6)
{
if(++sec>=6)PORTC=0x05; //6sec이후 // pump1(PC3) Off, pump2(PC2) on, valve(PC1) On
}
//
flowrate=pulsecount/calibrationfactor; //L/min
if(flowrate==0)
{
PORTD|= 2; //PD1 High Output (Water flow signal)
}
else PORTD&=~2; //PD1 Low Output
pulsecount = 0;
}
}
//
ISR (INT0_vect){ pulsecount++; }
//
int main(void){
char water[20]={0};
init_port();
init_timer1();
MCUCR |= (0 << ISC00)|(1 << ISC01);
GICR |= (1 << INT0);
sei();
/* Replace with your application code */
while (1){
sprintf(water,"WATERFLOW:%3f",flowrate);
Write_Command_LCD(0x08);
String_LCD(water);
_delay_ms(10);
}
}
함수 위치를 변경 해주면 함수 프로토타입 선언을 생략 할 수 있습니다.
댓글 0
조회수 1,251등록된 댓글이 없습니다.