BASIC4MCU | 질문게시판 | 코드비젼 소스코드 avr소스코드로 변환부탁드립니다
페이지 정보
작성자 전자초짜 작성일2018-06-01 03:01 조회8,028회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=1310
본문
#include <stdio.h>
#include <delay.h>
#include <mega128.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
// Declare your global variables here
#if 1 // -------------------------- KTKTKT
#define uint8_t int // BUGS
#define FeedTime 62*InputFeedTime // Atmega는 16Mhz이고 256클럭마다 타이머가 오버플로우하게 된다.
// 그리고 클럭 스케일을 clk/1024로 해놓았으므로 1초간 오버플로우 하는 횟수는
// 16,000,000/256/1024 = 61.03515625번 이므로 약 62번 오버 플로우하게 되면
// 1초를 맞출수 있게 된다.
#define AirTime 62*InputAirTime
#define DEBOUNCE_MAKE_DELAY 10 /* milliseconds */
/* Define Variable for Keypad */
#define Key_1 0x09
#define Key_2 0x0A
#define Key_3 0x0C
#define Key_4 0x11
#define Key_5 0x12
#define Key_6 0x14
#define Key_7 0x21
#define Key_8 0x22
#define Key_9 0x24
#define Key_0 0x42
#define Key_Star 0x41
#define Key_Sharp 0x44
/* Public Variable */
int Temperature=0;
unsigned int InputFeedTime = 0;
unsigned int InputAirTime = 0;
unsigned int TimesOverFlow = 0;
unsigned int FeedingStart = 0;
unsigned int FirstDutyTime = 0;
unsigned int SecondDutyTime = 0;
/* Functions */
/* 외부 인터럽트 허용 및 트리거 방식 설정 */
void InitExternalInterrupt (void)
{
// Set the Register of Interrupt
EICRA = 0x07;//_BV(ISC01) | _BV(ISC00) | _BV(ISC11); // INT0 use Rising edge to make trigger.
// INT1 use Falling edge to make trigger.
EICRB = 0x3f;//_BV(ISC41) | _BV(ISC40) | _BV(ISC51) | _BV(ISC50) | _BV(ISC61) | _BV(ISC60); // Set Logic level Signal trigger INT 7-4
EIMSK = 0x73; // We use five of INT Interupt, they are xooo xxoo.
// xooo -> INT 4,5,6 -> Keypad, xx00 -> INT 1,2 -> Temperature sensor.
}
/* LCD에 현재 온도를 표시 */
void DisplayTemperature (void)
{
/* LCD code to display temperature of water */
float DutyCycle;
int T10=0;
int T1=0;
DutyCycle = FirstDutyTime / SecondDutyTime;
Temperature = ( DutyCycle*100000 - 32000 ) / (470);
lcd_gotoxy(0,1);
lcd_putsf("Temperature:");
delay_ms(100);
T10 = Temperature/10;
T1 = Temperature - (T10 * 10);
lcd_gotoxy(14,0);
lcd_putchar((char)T10);
lcd_gotoxy(15,0);
lcd_putchar((char)T1);
}
/* Interrupts */
#if 1
interrupt [TIM2_OVF] void ext_signaloverflow_isr(void)
#else
INTERRUPT(SIG_OVERFLOW2)
#endif
{
uint8_t Temp_PortB;
/* 정해진 주기가 되면 인터랍트 발생 그후에 Feeding mode를 Enable 합니다. */
if ( 0 == (TimesOverFlow % FeedTime) )
{
FeedingStart = 1;
}
/* 정해진 주기가 되면 인터럽트 발생, 그후에 산소공급기를 켭니다. 다음 주기까지 지속. */
if ( 0 == (TimesOverFlow % AirTime) )
{
/* Turn On the Oxygen Generator */
Temp_PortB = PORTB && 0x02;
if ( Temp_PortB )
{
PORTB = PORTB && 0xfd;
}
else
{
lcd_gotoxy(0,0);
lcd_putsf("-Providing AIR -");
delay_ms(100);
PORTB = PORTB || 0x02;
}
}
TimesOverFlow++;
}
/* Get the Signal from Temperature sensor ____ PORTD (input) */
#if 1
interrupt [EXT_INT0] void ext_int0_isr(void)
#else
/* 온도 센서가 DutyCycle을 만들면 두개의 포트가 Rising edge와 Falling edge를 검색후 인터럽트 발생,
Duty Cycle를 계산합니다. */
INTERRUPT(SIG_INTERRUPT0) // It occurs when Temperature sensor makes rising edge of pulse.
#endif
{
unsigned int Temp_TCNT2;
Temp_TCNT2 = TCNT2; // Save the count of Timer
SecondDutyTime = Temp_TCNT2;
TCNT0 = 0x0000;
//DisplayTemperature();
}
#if 1
interrupt [EXT_INT1] void ext_int1_isr(void)
#else // #if 1 BUGS
INTERRUPT(SIG_INTERRUPT1) // It occurs when Temperature sensor makes falling edge of pulse.
#endif // #if 1
{
unsigned int Temp_TCNT0;
Temp_TCNT0 = TCNT0; // Save the count of Timer
FirstDutyTime = Temp_TCNT0;
}
#if 1
interrupt [EXT_INT4] void ext_int4_isr(void)
#else // #if 1 BUGS
/* 키패드 중에 별표를 누르면, 인터럽트가 발생하여 메뉴설정 모드로 들어가게 됩니다.
메뉴 설정중에는 모든 인터럽트가 금지되어 있으므로 안전하게 값들을 설정 가능해집니다.
#버튼을 누르면 설정이 종료됩니다. */
SIGNAL(SIG_INTERRUPT4) // It occurs when Keys of first column of keypad are pushed.
#endif // #if 1
{
int i,j,k;
int KeyInput[20];
int PlaceNum, TenTimes, TempNum;
if ( PINE == 0x41 ) // it means that '*'key is pushed and the user want to adjust values.
{
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
if ( PINE == 0x00 ) // To push anther key, the user has to release first key.
{ // At that time, PINE would be 0x00.
k=0; // Initialize array number.( We have to add LCD Display fuction)
TenTimes=1;
TempNum = 0;
for (;;)
{
if ( k == 1)
{
/* Display Select mode on LCD */
}
if ( PINE == Key_1 )
{
KeyInput[k] = 1;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_2 )
{
KeyInput[k] = 2;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_3 )
{
KeyInput[k] = 3;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_4 )
{
KeyInput[k] = 4;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_5 )
{
KeyInput[k] = 5;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_6 )
{
KeyInput[k] = 6;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_7 )
{
KeyInput[k] = 7;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_8 )
{
KeyInput[k] = 8;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_9 )
{
KeyInput[k] = 9;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_0 )
{
KeyInput[k] = 0;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
k++;
}
if ( PINE == Key_Star )
{
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
/* Add display CCC message on LCD */
k=0;
}
if ( PINE == Key_Sharp )
{
KeyInput[k] = 999;
for (i=0; i < DEBOUNCE_MAKE_DELAY ; i++ )
{ // This If sentence was made to debounce.
delay_ms(1); //If the switch bounce while only 10ms, It is Ok.
//Because It doesn't receive any signal while 10ms
}
if ( (KeyInput[0] == 1) || (KeyInput[0] ==2) )
{
for ( i=1 ; i<k ; i++)
{
PlaceNum = k-i-1;
for ( j=0, TenTimes=1; j < PlaceNum ; j++)
{
TenTimes = TenTimes * 10;
}
TempNum = TempNum + KeyInput[i] * TenTimes;
}
if ( KeyInput[0] == 1 )
{
InputFeedTime = TempNum;
}
if ( KeyInput[0] == 2 )
{
InputAirTime = TempNum;
}
break;
}
else
{
/* Add display "Input Error" message on LCD */
k=0;
}
}
}
}
}
}
#endif // #if 1
void main(void)
{
uint8_t input = 0x00;
uint8_t output = 0xff;
int j,i,count; // BUGS : MOVE MOVE
/* Default Value */
InputFeedTime = 3;
InputAirTime = 6;
TimesOverFlow = 0;
FeedingStart = 1;
FirstDutyTime = 1;
SecondDutyTime = 1;
/* Initialize Ports */
DDRA = output; // LCD
PORTA = 0x00;
DDRB = output; // Motor (PORTB 0bit) + Oxygen generator ( PORTB 1bit )
PORTB = 0x00;
DDRC = output; // LCD
PORTC = 0x00;
DDRD = input; // Tempeature sensor (PORTD 0bit and 1bit) - INT0 and INT1
DDRE = input; // Keypad
/* initialize External Signal interrupt */
InitExternalInterrupt();
/* Initialize Timer0, Timer2 funtions */
TCCR0 = 0x05; // Adjust Timer Scale -> clk/32
TCNT0 = 0xfc; // Timer Starts to counter from 254, it prevents making Assembly language problem.
TCCR2 = 0x05; // Adjust Timer Sclae -> clk/1024
TCNT2 = 0xfc;
TIMSK = 0x40; // Enable Timer/Couner0 Overflow Interrupt Enable
// Global enable interrupts
#asm("sei")
// LCD module initialization
lcd_init(16);
// After LCD initialize, Display Some words...
lcd_gotoxy(0,0);
lcd_putsf("**OH DONG HOON**");
delay_ms(100);
lcd_gotoxy(0,1);
lcd_putsf("AUTO FISH BASIN*");
delay_ms(100);
while (1)
{
/* 온도를 LCD에 표시 */
DisplayTemperature();
/* 일정 온도 범위를 넘어서면 온도 제어를 합니다. */
if(Temperature < 10)
PORTB |= 0x04; // 히터를 켭니다.
else if(Temperature > 20)
PORTB |= 0x08; // 쿨러를 켭니다.
else if((Temperature >= 10) && (Temperature <= 20))
PORTB &= 0xF3; // 히터랑 쿨러 모두다 끕니다.
/* Feeding Mode 실행 */
if ( FeedingStart )
{
lcd_gotoxy(0,0);
lcd_putsf("-- FEEDING --");
/* Making 20 Pulse for motor */
for (j=0; j<=20; j++)
{
PORTB |= 0x01; /* output 1 to port1.0 */
delay_ms(2); /* 1mSec delay*/
PORTB &= 0xfe; /* output 0 to port1.0*/
delay_ms(4); /* 4mSec delay*/
}
FeedingStart =0; // ---- 0 -----
}
};
}
코드비젼 소스코드를 avr 소스코드로 바꾸려는데 컴파일시 오류가 너무 많네요.
어떻게 바꿔야하는지도 감이안잡히는데 도움부탁드립니다.
댓글 0
조회수 8,028등록된 댓글이 없습니다.