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

📄 m16_pwm.c.bak

📁 AVR单片机ATmega16的PWM测试程序
💻 BAK
字号:
#include <avr/io.h>
#include <util/delay.h>
#include "my_type.h"

uint8 table[12] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x79};
uint8 data[4]  = {10,0,5,0};
uint8 key1, key2;

void display(uint8 *p)
{
		uint8 sel = 0x01;
		for (uint8 i = 0; i < 4; i++)
		{
			PORTC = sel;
			PORTA = 0xff - table[p[i]];
			_delay_ms(10);
			sel <<= 1;	
		}
}

void add(uint8 *p)
{
	if (p[1] != 1)
	{
		p[2]++;
		if (p[2] == 10)
		{
			p[2] = 0;
			p[1]++;		
		}			
	}	
}

void sub(uint8 *p)
{
	uint8 tmp;
	tmp = p[1] * 10 + p[2];
	if (tmp != 0)
	{
		tmp -= 1;
		p[1] = tmp / 10;
		p[2] = tmp % 10;	
	}	
}

void key_process(void)
{
		while ((PINB & 0x01) == 0)
		{
			display(data);
			key1 = 1;
		}
		while ((PINB & 0x02) == 0)
		{
			display(data);
			key2 = 1;	
		}
		
		if (key1 == 1)
		{
			add(data);
			key1 = 0;	
		}
		if (key2 == 1)
		{
			sub(data);
			key2 = 0;	
		}
}

void set_process(uint8 *p)
{
	uint16 tmp;
	tmp = p[1] * 10 + p[2];
	tmp = 1023 * tmp / 10;
	OCR1AH = tmp >> 8;
	OCR1AL = tmp & 0x00ff;
}

void io_init(void)
{
	DDRA = 0xff;
	PORTA = 0xff;
	DDRC = 0xff;
	PORTC = 0xff;	
	DDRB = 0x00;
	PORTB = 0x00;
	DDRD = 0xff;
	PORTD = 0xff;
}


int main(void)
{
	io_init();
	TCCR1A = 0xc3;
	TCCR1B = 0x02;
	_delay_ms(100);
	while (1)
	{
		key_process();
		set_process(data);
		display(data);	
	}
	return 0;	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -