单片机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

数控电源AVR单片机C程序

热度 1已有 980 次阅读2016-2-4 02:12 | 单片机, 程序, 电源, 数控

编译环境为CodeWizardAVR

#include <mega8.h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>

#define Voltage_UP PINB.0
#define Voltage_Down PINB.2
#define UD PORTB.3
#define CS PORTB.5
#define INC PORTB.4
#define Voltage_LED PORTB.6
#define Current_LED PORTB.7

#define Current_Detect 0
#define Out_Detect 1
#define Display_DP PORTC.2
#define Display_D3 PORTC.3
#define Display_D2 PORTC.4
#define Display_D1 PORTC.5

#define Current_UP PIND.2
#define Current_Down PIND.3

#define Display PORTD
#define AD_quotiety 0.014560//0.015856

#define ADC_VREF_TYPE 0xc0

unsigned char select_led = 1;
unsigned char Led_left;
unsigned char Led_middle;
unsigned char Led_right;
unsigned int read_adc(unsigned char adc_input)//参数为ADC通道选择
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
//used to display
TCNT0 = 0x00; //8Mhz时钟,1024分频,中断TCNT0重填0
if(select_led == 1)
{
Display = (Led_left << 4) | 0X0F;
Display_D1 = 0;
Display_D2 = 1;
Display_D3 = 1;
select_led = 2;
}
else if(select_led == 2)
{
Display = (Led_middle << 4) | 0X0F;
Display_D1 = 1;
Display_D2 = 0;
Display_D3 = 1;
select_led = 3;
}
else if(select_led == 3)
{
Display = (Led_right << 4) | 0X0F;
Display_D1 = 1;
Display_D2 = 1;
Display_D3 = 0;
select_led = 1;
}
}

void calculate_voltage(unsigned char adc_channel)
{
unsigned char Led_left_temp;
int adc_data;
unsigned int voltage_value;
adc_data = read_adc(adc_channel);
voltage_value = (int)(adc_data * AD_quotiety *100); //
Led_left = (voltage_value / 100);
voltage_value = voltage_value % 100; //15
if(Led_left >= 0x0a) // 11.5
{
Led_left_temp = Led_left;
Led_left = Led_left / 10;
Led_middle = Led_left_temp % 10;
Led_right = voltage_value / 10;
}
else // 1.15
{
Led_middle = voltage_value / 10;
Led_right = voltage_value % 10;
}
}

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{ //used to read adc
TCNT1H=0x00;
TCNT1L=0x00;
calculate_voltage(1);
}

void Wiper_Up(void)
{
INC = 1; //inc
CS = 0; //cs
UD = 1; //ud
delay_us(10);
INC = 0; //inc
delay_us(10);
CS = 1; //cs
delay_us(10);
}

void Wiper_down(void)
{
INC = 1; //inc
CS = 0; //cs
UD = 0; //ud
delay_us(10);
INC = 0; //inc
delay_us(10);
CS = 1; //cs
delay_us(10);
}

void save(void)
{
INC = 1; //inc
CS = 0; //cs
delay_us(10);
INC = 1; //inc
delay_us(10);
CS = 1; //cs
delay_us(10);
}

void port_init(void)
{
PORTB = 0xFF; DDRB = 0xFA;
//PORTB2.3输出PWM,要设为输出
PORTC = 0x3C; DDRC = 0x3C;
PORTD = 0xFF; DDRD = 0xF0;
}

void timer0_init(void)
{
// Timer/Counter 0 initialization
// Clock source: System Clock
TCCR0 = 0x03; //clk/1024
TCNT0 = 0x00;
TIMSK = 0x05; //open timer0 and timer1
}

void timer1_init(void)
{
// Timer/Counter 0 initialization
// Clock source: System Clock
TCCR1A=0x00;
TCCR1B=0x03; //clk/64
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
}

void uart_init(void)
{
// USART Baud rate: 19200
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
}

void adc_init(void)
{
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x87;
SFIOR&=0xEF;
SFIOR|=0x10;
ACSR=0x80;
}

void check_key(void)
{

if(Current_UP == 0)
{
Wiper_Up();
//calculate_voltage(1);
do
{
delay_ms(15);
}while(Current_UP == 0);
save();
}
else if(Current_Down == 0)
{
Wiper_down();
//calculate_voltage(1);
do
{
delay_ms(15);
}while(Current_Down == 0);
save();
}
else if(Voltage_UP == 0)
{
Wiper_Up();
do
{
delay_ms(15);
}while(Voltage_UP == 0);
save();
}
else if(Voltage_Down == 0)
{
Wiper_down();
do
{
delay_ms(15);
}while(Voltage_Down == 0);
save();
}
}

void main(void)
{

port_init();
timer0_init();
timer1_init();
uart_init();
adc_init();
#asm("sei")

while(1)
{
check_key();
};
}

路过

鸡蛋

鲜花
1

握手

雷人

刚表态过的朋友 (1 人)

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

QQ|手机版|小黑屋|单片机论坛 |51Hei单片机16群 联系QQ:125739409;技术交流QQ群7344883

Powered by 单片机教程网

返回顶部