⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main__.c

📁 智能充电参考程序 可以控制整个充电过程希望对大家有帮助
💻 C
📖 第 1 页 / 共 5 页
字号:
/****************************************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 + -