📄 solar power.c
字号:
#include <90s8515.h>
#include <stdio.h>
#include <lcd.h>
//timeout values for each task
#define t1 50
#define t2 100
#define timeout_time 36000 // 3 mins at 5ms tick
#define LCDwidth 16 //characters
#asm
.equ __lcd_port = 0x12
#endasm
// the task subroutines
void task1(void);
void task2(void);
void initialize(void); // all the usual mcu stuff
void new_rx(void);
char lcd_buffer[17]; // LCD display buffer
unsigned int lcd_time;
unsigned int timeout; // no valid rx in timeout_time
unsigned char time1;
unsigned char time2;
unsigned int timer1; // max rx time plus some (1.2 sec)
unsigned int timer2;
unsigned char rx_error; // success of last rx
unsigned char rx_index; // state information
unsigned char voltage, soc, out_temp, bat_temp;
unsigned char alarm; // 0 - Disarmed
// 1 - Activated
// 2 - Armed
unsigned char charging; // 0 - Nothing
// 1 - Charging
// 2 - Discharging
// 3 - Charging and Discharging
//**********************************************************
//timer 1 compare ISR
interrupt [TIM1_COMPA] void cmpA_overflow(void)
{
if (time1 > 0)
time1--;
if (time2 > 0)
time2 = time2 - 1;
if (timer1 > 0)
timer1--;
if (timer2 > 0)
timer2--;
//if ((rx_error == 1) && (timeout <= timeout_time))
// timeout++;
//if (rx_error == 0)
// timeout = 0;
if (timeout <= timeout_time)
timeout++;
lcd_time++; // Flash alternate screens
}
//**********************************************************
//Entry point and task scheduler loop
void main(void)
{
initialize();
lcd_clear();
//main task scheduler loop
while(1)
{
lcd_gotoxy(0, 0);
if (lcd_time < 600)
{
if (charging == 1)
sprintf(lcd_buffer, "Voltage %2d.%1d + ", voltage >> 4, voltage &
0x0f);
else if (charging == 2)
sprintf(lcd_buffer, "Voltage %2d.%1d -", voltage >> 4, voltage &
0x0f);
else if (charging == 3)
sprintf(lcd_buffer, "Voltage %2d.%1d +-", voltage >> 4, voltage &
0x0f);
else
sprintf(lcd_buffer, "Voltage %2d.%1d ", voltage >> 4, voltage &
0x0f);
lcd_puts(lcd_buffer);
lcd_gotoxy(0, 1);
if (alarm == 2)
sprintf(lcd_buffer, "Soc %2d%1d%% Armed", soc >> 4, soc & 0x0f);
else
sprintf(lcd_buffer, "Soc %2d%1d%% ", soc >> 4, soc & 0x0f);
lcd_puts(lcd_buffer);
}
else if (lcd_time < 1200)
{
sprintf(lcd_buffer, "Outside Tmp %2d%1dF", out_temp >> 4, out_temp &
0x0f);
lcd_puts(lcd_buffer);
lcd_gotoxy(0, 1);
sprintf(lcd_buffer, "Battery Tmp %2d%1dF", bat_temp >> 4, bat_temp &
0x0f);
lcd_puts(lcd_buffer);
}
else
{
lcd_time = 0;
}
if (PINC.7 == 1)
{
new_rx();
}
if ((alarm == 1) && (time1 == 0))
task1();
if ((timeout >= timeout_time) && (time2 == 0))
task2();
}
}
//**********************************************************
void task1(void)
{
time1 = t1;
PORTB.0 = ~PORTB.0; // Buzzer
PORTB.1 = ~PORTB.1; // RED LED
}
//**********************************************************
void task2(void)
{
time2 = t2;
PORTB.2 = ~PORTB.2; // Yellow LED
}
//**********************************************************
void new_rx(void)
{
unsigned char input;
unsigned char www;
timeout = 0; // timeout incremented in interrupt
// checked for timeout condition in main
input = PINC & 0x0f; // Read the input
while(PINC.7 == 1) // Wait until receive pin is cleared
{
www++;
}
switch (rx_index)
{
case 0: // Init
break;
case 1: // Reserved
break;
case 2: // Voltage High
voltage = input << 4;
rx_index = 3;
PORTA = 0x03; // Set Receive Address of Voltage Low
break;
case 3: // Voltage Low
voltage = voltage | input;
rx_index = 4;
PORTA = 0x04;
break;
case 4: // Soc High
soc = input << 4;
rx_index = 5;
PORTA = 0x05;
break;
case 5: // Soc Low
soc = soc | input;
rx_index = 6;
PORTA = 0x06;
break;
case 6: // Out Temp H
out_temp = input << 4;
rx_index = 7;
PORTA = 0x07;
break;
case 7: // Out Temp L
out_temp = out_temp | input;
rx_index = 8;
PORTA = 0x08;
break;
case 8: // Bat Temp H
bat_temp = input << 4;
rx_index = 9;
PORTA = 0x09;
break;
case 9: // Bat Temp L
bat_temp = bat_temp | input;
rx_index = 0x0a;
PORTA = 0x0a;
break;
case 0x0a: // Charging/Alarm
charging = input >> 2;
alarm = input & 0x03;
rx_index = 2;
PORTA = 0x02;
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
case 15:
break;
}
timer1 = 100; // Delay for decoder transition 0.5 sec
while(timer1 > 0)
{
www++;
}
}
//**********************************************************
//Set it all up
void initialize(void)
{
lcd_init(LCDwidth); // initialize the display
// Port D is LCD
//set up the ports
DDRB = 0xff; // PORT B is an output
// B.0 = Buzzer Active High
// B.1 = Red LED Active High
// B.2 = Orange LED Active High
DDRC = 0x00; // PORT C is an input
PORTC = 0x00; // Hi-Z inputs
// C.3 - C.0 = Din
// C.7 = Incoming Data
PORTB = 0x00;
DDRA = 0xff; // PORT A is an output
// A.3 - A.0 Address Control
PORTA = 0x02; // initial value
rx_index = 2; // initial value
// Set up timer 1 to tick at 5ms
TIMSK = 0x40; // turn on timer 1 compare match interrupt
TCCR1A = 0x00;
TCCR1B = 0x0a; // sets prescale to 8, clear on compare match
TCNT1 = 0; // and zero the timer
OCR1A = 2500; // 0.25us * 8 * 2500 = 5ms sec
time1 = t1; // alarm time
time2 = t2; // rx timeout time
rx_error = 1;
timer1 = 0;
timer2 = 0;
//crank up the ISRs
#asm
sei
#endasm
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -