?? main__.c
字號:
/****************************************Copyright (c)**************************************************
** 智 能 充 電 器 開 發 小 組
** OurAVR 論壇
** QQ 群: 26052247
**
** http://www.ouravr.com/bbs
**
** This program was produced by the
** CodeWizardAVR V1.24.7d Professional
** Copyright 2008-2009 OurAVR
** Chip type : ATmega16
** Program type : Application
** Clock frequency : 16.000000 MHz
** Memory model : Small
** External SRAM size : 0
** Data Stack size : 256
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: Main.c
**創 建 人: 呂海安
**最后修改日期: 2008年03月02日
**描 述: 智能充電器軟件
**
**--------------歷史版本信息----------------------------------------------------------------------------
** 創建人: 呂海安
** 版 本: v0.01
** 日 期: 2008年03月02日
** 描 述: 原始版本
**
**--------------當前版本修訂------------------------------------------------------------------------------
** 修改人:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include "config.h"
#ifndef __SLEEP_DEFINED__
#define __SLEEP_DEFINED__
.EQU __se_bit=0x40
.EQU __sm_mask=0xB0
.EQU __sm_powerdown=0x20
.EQU __sm_powersave=0x30
.EQU __sm_standby=0xA0
.EQU __sm_ext_standby=0xB0
.EQU __sm_adc_noise_red=0x10
.SET power_ctrl_reg=mcucr
#endif
// Declare your global variables here
INT8U P10msReq;
INT8U P100msReq;
INT8U para[230]; // 所有參數和全局變量表
/*********************************************************************************************************
** 函數名稱: timer0_ovf_isr
** 功能描述: Timer 0 overflow interrupt service routine,16.384 mS @ 16M
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
static INT16U t0count1;
Disable();
t0count1++;
P10msReq = 1; // 16.384 mS
if(t0count1 >= 6) // about 100 mS
{
t0count1 = 0;
P100msReq = 1;
}
Enable();
}
/*********************************************************************************************************
** 函數名稱: InitCPU
** 功能描述: 初始化I/O端口和各寄存器
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
void InitCPU(void)
{
PORTB = 0x00;
DDRB = 0x08;
PORTC = 0x00;
DDRC = 0xC3;
PORTD = 0x10;
DDRD = 0xB0;
lcd_init(); // 液晶初始化
KEY_init(); // 按鍵初始化
pwm_init(); // PWM 初始化
s_analog_init(); // ADC 初始化
usart_init(); // 串口初始化
}
/*********************************************************************************************************
** 函數名稱: p10ms
** 功能描述: 10 MS掃描程序
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
void p10ms(void)
{
//cell_output(); // 調用PWM輸出控制程序
}
/*********************************************************************************************************
** 函數名稱: p100ms
** 功能描述: 10 MS掃描程序
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
void p100ms(void)
{
static INT16U timecnt = 0;
INT8U i;
for(i=0; i<7; i++)
{
filter_adc(i);
}
if(timecnt % 2 == 0)
{
LED1 = !LED1;
}
ShowMenu(); // 顯示菜單程序
/*
lcd_locate(0,0);
lcd_print_number((((INT16U)Bat0_Vol_H << 8) + Bat0_Vol_L),4,0);
lcd_locate(0,1);
lcd_print_number((((INT16U)Bat0_Cur_H << 8) + Bat0_Cur_L),4,0);
lcd_locate(8,0);
lcd_print_number((((INT16U)Bat1_Vol_H << 8) + Bat1_Vol_L),4,0);
lcd_locate(8,1);
lcd_print_number((((INT16U)Bat1_Cur_H << 8) + Bat1_Cur_L),4,0);
*/
if(timecnt >= 60000)
{
timecnt = 0;
}
timecnt ++;
}
/*********************************************************************************************************
** 函數名稱: main
** 功能描述: 主函數
********************************************************************************************************/
void main(void)
{
// Declare local variables here
//INT16U i;
delay_ms(1000); // 上電延時 1 S ,保證 MCU 正常工作
InitCPU(); // 初始化 CPU
lcd_locate(0,0);
lcd_print_stringF("OurOUR, Charger ");
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 1;
Enable(); // Global enable interrupts
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
/* //第一次探測,探知電池是否就位
for(i=0; i<7; i++)
{
filter_adc(i);
}
//電池探測,是否有電池,根據電壓探測
lcd_locate(0,0);
lcd_print_number(Memory_Char_to_Int(BAT_0_BASE + BAT_1_VOL - 1),4,0);
if(Memory_Char_to_Int(BAT_0_BASE+BAT_0_VOL-1)<4000)
lcd_print_stringF("BAT1 ready!");
else
lcd_print_stringF("BAT1 empty!");
lcd_locate(0,1);
if(Memory_Char_to_Int(BAT_1_BASE+BAT_1_VOL-1)<4000)
lcd_print_stringF("BAT0 ready!");
else
lcd_print_stringF("BAT0 empty!");
*/
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
//bat_discharge(BAT0, 50);
while (1)
{
if(P10msReq)
{
P10msReq = 0;
p10ms(); // 處理 10 mS 函數
}
if (P100msReq)
{
P100msReq = 0;
p100ms(); // 處理 100 mS 函數
}
/*
if(memory[BAT0_PWM_VOL_OR_CUR-1]==VOL)
{
lcd_locate(0,LINE_1);
lcd_print_stringF("Vol Control:");
lcd_locate(0,LINE_2);
lcd_print_number(Memory_Char_to_Int(BAT0_VOL_SET-1),4,0);
lcd_locate(6,LINE_2);
lcd_print_number(Memory_Char_to_Int(BAT_0_BASE+BAT_0_VOL-1),4,0);
}
else
{
lcd_locate(0,LINE_1);
lcd_print_stringF("Cur Control:");
lcd_locate(0,LINE_2);
lcd_print_number(Memory_Char_to_Int(BAT0_CUR_SET-1),4,0);
lcd_locate(6,LINE_2);
lcd_print_number(Memory_Char_to_Int(BAT_0_BASE+BAT_0_CUR-1),4,0);
//lcd_print_number(cell_pid0(),4,0);
// 當調試放電的時候,最好監控OCR0的實際PWM參數
// lcd_locate(0,LINE_2);
// lcd_print_number(OCR0,3,0);
}
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
*/
};
}
/********************************************************************************************************
** END OF FILE
********************************************************************************************************/
/****************************************Copyright (c)**************************************************
** 智 能 充 電 器 開 發 小 組
** OurAVR 論壇
** QQ 群: 26052247
**
** http://www.ouravr.com/bbs
**
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: UART.c
**創 建 人: 呂海安
**最后修改日期: 2007年01月13日
**描 述: UART的底層函數 FOR AVR MCU / Mega8
**
**--------------歷史版本信息----------------------------------------------------------------------------
** 創建人: 呂海安
** 版 本: v1.0
** 日 期: 2007年07月13日
** 描 述: 原始版本
**
**--------------當前版本修訂------------------------------------------------------------------------------
** 修改人: 呂海安
** 日 期: 2008年03月02日
** 描 述: For 智能充電器 FOR AVR MCU / Mega16
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include "config.h"
#ifndef __SLEEP_DEFINED__
#define __SLEEP_DEFINED__
.EQU __se_bit=0x40
.EQU __sm_mask=0xB0
.EQU __sm_powerdown=0x20
.EQU __sm_powersave=0x30
.EQU __sm_standby=0xA0
.EQU __sm_ext_standby=0xB0
.EQU __sm_adc_noise_red=0x10
.SET power_ctrl_reg=mcucr
#endif
#include "UART.h"
#define RX_BUFFER_SIZE 8
INT8S rx_buffer[RX_BUFFER_SIZE]; // USART Receiver buffer
#if RX_BUFFER_SIZE < 256
INT8U rx_wr_index,rx_rd_index,rx_counter;
#else
INT16U rx_wr_index,rx_rd_index,rx_counter;
#endif
bit rx_buffer_overflow; // This flag is set on USART Receiver buffer overflow
/*********************************************************************************************************
** 函數名稱: debug_usart_init
** 功能描述: 串口通信初始化, 38400
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
void debug_usart_init(void)
{
UCSRB = 0x00;
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x19;
UBRRH = 0x00;
UCSRB = 0x1C;
}
/*********************************************************************************************************
** 函數名稱: usart_init
** 功能描述: 波特率 38400, 8位,發送中斷模式,接收中斷模式
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
void usart_init(void)
{
UCSRB = 0x00;
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x19;
UBRRH = 0x00;
UCSRB = 0x9C;
}
/*********************************************************************************************************
** 函數名稱: usart_rx_isr
** 功能描述: USART Receiver interrupt service routine
** 輸入參數: 無
** 輸出參數: 無
********************************************************************************************************/
interrupt [USART_RXC] void usart_rx_isr(void)
{
INT8S status,data;
status = UCSRA;
data = UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN)) == 0)
{
rx_buffer[rx_wr_index] = data;
if (++rx_wr_index == RX_BUFFER_SIZE)
{
rx_wr_index=0;
}
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter = 0;
rx_buffer_overflow = 1;
};
};
}
/*********************************************************************************************************
** 函數名稱: getchar
** 功能描述: Get a character from the USART Receiver buffer
** 輸入參數: 無
** 輸出參數: INT8S data: 串口接收緩存
********************************************************************************************************/
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_GETCHAR_
#pragma used+
INT8S getchar(void)
{
INT8S data;
while (rx_counter == 0);
data = rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE)
{
rx_rd_index = 0;
}
Disable(); // 關中斷
--rx_counter;
Enable(); // 開中斷
return data;
}
#pragma used-
#endif
#define TX_BUFFER_SIZE 8
INT8S tx_buffer[TX_BUFFER_SIZE]; // USART Transmitter buffer
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -