BASIC4MCU | 질문게시판 | atmega128 0 9999 업카운터
페이지 정보
작성자 hapy 작성일2020-11-13 12:17 조회7,363회 댓글3건본문
/*******************************************************************************
LCD Character Display Test Program
Digital Watch Test Program by using delay() function
Hardware Interface:
PORTA(1-4) <-------> CN31(1-4): LCD CONTROL SIGNAL
PORTA(5-8) <-------> CN32(5-8): LCD DATA LINE
Version: 0.2
Date: 2020. 11. 13
Program:
Chip type : ATmega128
Program type : Application
Clock frequency : 16.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 1024
*******************************************************************************/
#include <mega128.h>
#include <delay.h>
#include <stdio.h> /* for sprintf() function */
#include <lcd.h> /* for LCD function */
/* Alphanumeric LCD Module functions */
#asm
.equ __lcd_port = 0x1B; /* Port A */
#endasm
#define ENABLE (PORTA |= 0x04) /* LCD ENable or PORTA_Bit2=1; */
#define DISABLE (PORTA &= 0xFB) /* LCD DIsable or PORTA_Bit2=0; */
#define BYTE unsigned char
#define WORD unsigned int
#define UINT unsigned int
/* Data for the FND Selection */
BYTE FND_selector[4]={0x7F, 0xBF, 0xDF, 0xEF};
/* Output Display Data on FND: Common Anode Type */
BYTE FND_Data[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90};
/*****************************************************************/
/* Description for Interface PORTA with LCD */
/* */
/* PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 */
/* ---------------------------------------------------------- */
/* | D7 | D6 | D5 | D4 | x | EN | R//W | RS | */
/* ---------------------------------------------------------- */
/* */
/* x - Don't care (unused) */
/* */
/*****************************************************************/
void Initialize_PORTA(void)
{
DDRA = 0xFF; /* Set PORTA to Output Mode */
PORTA = 0xFF; /* Initialize PORTA */
DDRB = 0xFF; /* Set PORT B to output Mode*/
PORTB = 0xFF;
DDRD = 0xFF; /* Set PORT D to output Mode */
PORTD = 0xFF;
}
/* Instruction(or Command) Write Function */
void Instruction_Output(BYTE b)
{
PORTA= b & 0xF0; /* Output High 4BIT */
ENABLE; /* LCD ENABLE (or EN) */
DISABLE; /* LCD DISABLE(or EN) */
PORTA=(b<<4) & 0xF0; /* Output Low 4BIT */
ENABLE; /* LCD ENABLE(or EN) */
DISABLE; /* LCD DISABLE(or EN) */
/* Delay until Complete Instruction(or Command) execution */
delay_ms(2);
}
void Character_Output(BYTE b) /* Output function one Character to LCD */
{ /* Wait_Busy_Flag(); */
PORTA=(b & 0xF0) | 0x01; /* Output High 4BIT */
ENABLE; /* LCD ENABLE(or EN) */
DISABLE; /* LCD DISABLE(or EN) */
PORTA=((b<<4) & 0xF0) | 0x01; /* Output Low 4BIT */
ENABLE; /* LCD ENABLE(or EN) */
DISABLE; /* LCD DISABLE(or EN) */
/* Delay until Complete Instruction(or Command) execution */
delay_ms(2);
}
void String_Output(BYTE b, flash BYTE *string) /* Function for Output Character String */
{
BYTE i=0;
Instruction_Output(b); /* Set LCD Position */
do
{
Instruction_Output(0xC0);
delay_ms(5);
Character_Output(string[i]);
delay_ms(5);
}while(string[++i] !='\0'); /* Check End of Character (that is, until NULL character) */
}
/*****************************************************/
/* Initialize LCD Routine */
/* Ref. Instruction(or Command) Table */
/*****************************************************/
void Initialize_LCD(void)
{
Initialize_PORTA(); /* Initialize LCD Interface PORTA */
Instruction_Output(0x01); /* LCD CLEAR */
delay_ms(5);
Instruction_Output(0x28); /* LCD FUNCTION SET(16X2 LINE, Data Line 4 BIT Interface, 5X7 DOT) */
delay_ms(5);
// Instruction_Output(0x38); /* LCD DISPLAY ON, CURSOR OFF, BLINK OFF, DATA Line 8bit Interface */
Instruction_Output(0x06); /* LCD ENTRY MODE SET(Character Right Shift) */
delay_ms(5);
Instruction_Output(0x0C); /* LCD Display ON, CURSOR OFF, BLINK OFF */
delay_ms(5);
}
void main(void)
{
WORD count = 0;
char string_buffer[50];
lcd_init(16);
// Initialize_LCD();
BYTE i, j;
BYTE FND_display[4];
UINT FND_count=0, temp;
while(1)
{
count++;
Instruction_Output(0x01); /* lcd_clear(); */
delay_ms(5);
lcd_gotoxy(0,0);
sprintf(string_buffer, " Counter Value ");
lcd_puts(string_buffer);
delay_ms(400);
/********************************************************************
여기서 LCD에 0에서 9999까지 업카운트 값을 표시하는 프로그램을 작성하시오.
단, 마지막 9999까지 표현 했으면 다시 0에서 9999까지 업카운터를 무한 반복하도록
프로그램을 작성하면 됩니다.
*********************************************************************/
FND_count++; /* Increase value to display to FND */
if(FND_count==0x9999) FND_count = 0;
temp = FND_count;
for(i=0; i<4; i++)
{
FND_display[i] = temp % 9; /* Save FND_count */
temp = temp/9;
}
for(j=0; j<13; j++)
{
for(i=0; i<4; i++) /* This for statement is for a delay effect */
{
PORTD = FND_Data[FND_display[i]];
PORTB = FND_selector[i];
delay_ms(1);
/* Very Important: FND off, if FND is not off, */
/* The display number will be show overlap. */
PORTB = 0xFF;
delay_ms(1);
}
}
lcd_gotoxy(0,1);
sprintf(string_buffer, " %d ", count);
lcd_puts(string_buffer);
delay_ms(1000);
}
}
원래 프로그램에 0 9999 업카운터를 하려고 하는데 변수 i 에 대해서 오류(must declare first in block)가 발생하는 해결방안을 잘 모르겠습니다.
댓글 3
조회수 7,363master님의 댓글
master 작성일
전역변수 선언은 함수의 위에서 선언해야 하므로 헤더파일 아래에 보통 많이 위치합니다.
로컬변수 선언은 함수명 바로 아래에서 선언합니다.
void main(void){
WORD count = 0;
char string_buffer[50];
예를들면 위 2 라인은 위치가 서로 바뀌어야 합니다.
master님의 댓글
master 작성일
BYTE i, j;
BYTE FND_display[4];
UINT FND_count=0, temp;
이 선언들도 아래에 있군요 위로 올리세요
void main(void){
char string_buffer[50];
BYTE i, j;
BYTE FND_display[4];
UINT FND_count=0, temp;
WORD count = 0;
이런식으로 선언해야 합니다.
hapy님의 댓글
hapy 작성일항상 감사합니다.