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

📄 mp3.c

📁 AVR+CH375+VS1003
💻 C
📖 第 1 页 / 共 3 页
字号:
	//public void delay_100ms( uint8 data )
	
	//延时1s
	//public void delay_1s( uint8 data )
}
//*******************************************************
//LED发光二极管元件
unit led
{
	//初始化
	public void init()
	{
		LED_ddr = high;
		set_LED( false );
	}
	//红灯闪烁
	public void flash( uint8 t )
	{
		loop( t ) {
			set_LED( true );
			time.delay_10ms( 10 );
			set_LED( false );
			time.delay_10ms( 10 );
		}
	}
	//设置红色LED状态
	public void set_LED( bool is_light )
	{
		if( is_light ) {
			LED_port = high;
		}
		else {
			LED_port = low;
		}
	}
	//端口
	bit LED_port = @sfr.PORTB.4;
	bit LED_ddr = @sfr.DDRB.4;
}
//*******************************************************
//屏幕类
unit screen
{
	//初始化
	public void init()
	{
		nokia5110.init();
	}
	//LCD清屏函数
	public void clear()
	{
		nokia5110.set_location( 0, 0 );
		loop( 6 ) {
			loop( 84 ) {
				nokia5110.write_data( 0 );
			}
		}
	}
	//显示缓冲区数据
	public void show_buffer( *uint8 buffer )
	{
		uint8 line = 0;
		uint8 column = 0;
		uint16 index = 0;
		loop( 48 ) {
			uint8 d = buffer[index];
			screen.put_char( line, column, hex_table[d >> 4] );
			column += 5;
			screen.put_char( line, column, hex_table[d & 0x0f] );
			column += 5;
			++index;
			if( index.0[uint8] % 2 == 0 ) {
				++column;
			}
			if( index.0[uint8] % 4 == 0 ) {
				++column;
			}
			if( index.0[uint8] % 8 == 0 ) {
				++line;
				column = 0;
			}
		}
	}
	//在某处显示一个十六进制数字
	public void put_hex_number( uint8 line, uint8 column, uint32 hex )
	{
		column += 35;
		loop( 8 ) {
			screen.put_char( line, column, hex_table[(uint8)hex & 0x0f] );
			hex >>= 4;
			column -= 5;
		}
	}
	//在某处显示一个十六进制数字
	public void put_hex_byte( uint8 line, uint8 column, uint8 hex )
	{
		column += 5;
		loop( 2 ) {
			screen.put_char( line, column, hex_table[hex & 0x0f] );
			hex >>= 4;
			column -= 5;
		}
	}
	//在某处显示一个字符串常量
	public void put_const_string( uint8 line, uint8 column, *code uint8 s )
	{
		while( *s != 0 ) {
			put_char( line, column, *s );
			++s;
			column += 5;
		}
	}
	//在某处显示一个字符串变量
	public void put_ram_string( uint8 line, uint8 column, *uint8 s )
	{
		while( *s != 0 ) {
			put_char( line, column, *s );
			++s;
			column += 5;
		}
	}
	//在某处显示一个字符
	public void put_char( uint8 line, uint8 column, uint8 char )
	{
		nokia5110.set_location( line, column );
		*code uint8 p = ASCII.get_char_handle( char );
		loop( 5 ) {
			nokia5110.write_data( *p );
			++p;
		}
	}
	[16]code uint8 hex_table = {
		'0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
}
//*******************************************************
//NOKIA5110液晶屏元件
unit nokia5110
{
	//初始化
	public void init()
	{
		RST_ddr = high;
		CE_ddr = high;
		DC_ddr = high;
		Din_ddr = high;
		SCK_ddr = high;
		
		CE_port = high;
		SCK_port = high;
		
		RST_port = low;
		time.delay_10ms( 1 );
		RST_port = high;
		
		write_comand( 0x21 );	//LCD功能设置:芯片活动,水平寻址,使用扩展指令
 		write_comand( 0xd0 );	//设置VOP值
 		write_comand( 0x20 );	//LCD功能设置:芯片活动,水平寻址,使用基本指令
 		write_comand( 0x0C );	//设定显示配置:普通模式
	}
	//设置当前行列 column-0~83  line-0~5
	public void set_location( uint8 line, uint8 column )
	{
		write_comand( 0x40 | line );
		write_comand( 0x80 | column );
	}
	//写一个数据
	public void write_data( uint8 data )
	{
		CE_port = low;
		DC_port = high;
		loop( 8 ) {
			Din_port = data.7;
			data <<= 1;
			SCK_port = low;
			SCK_port = high;
		}
		CE_port = high;
	}
	//写一个命令
	void write_comand( uint8 cmd )
	{
		CE_port = low;
		DC_port = low;
		loop( 8 ) {
			Din_port = cmd.7;
			cmd <<= 1;
			SCK_port = low;
			SCK_port = high;
		}
		CE_port = high;
	}
	bit RST_port = @sfr.PORTD.7;
	//bit RST_pin = @sfr.PIND.7;
	bit RST_ddr = @sfr.DDRD.7;
	
	bit CE_port = @sfr.PORTD.6;
	//bit RST_pin = @sfr.PIND.6;
	bit CE_ddr = @sfr.DDRD.6;
	
	bit DC_port = @sfr.PORTD.5;
	//bit RST_pin = @sfr.PIND.5;
	bit DC_ddr = @sfr.DDRD.5;
	
	bit Din_port = @sfr.PORTD.4;
	//bit Din_pin = @sfr.PIND.4;
	bit Din_ddr = @sfr.DDRD.4;
	
	bit SCK_port = @sfr.PORTD.3;
	//bit SCK_pin = @sfr.PIND.3;
	bit SCK_ddr = @sfr.DDRD.3;
}
//*******************************************************
//ASC-II码字库
unit ASCII
{
	//获取某个字符的ASCII字形码索引
	public *code uint8 get_char_handle( uint8 char )
	{
		if( char < 0x20 ) {
			return icon['!' - 0x20];
		}
		if( char > 0x7f ) {
			return icon['?' - 0x20];
		}
		return icon[char - 0x20];
	}
	[96][5]code uint8 icon = {
		0x00,0x00,0x00,0x00,0x00,// 0
		0x00,0x00,0x5F,0x00,0x00,//!1
		0x00,0x03,0x00,0x03,0x00,//"2
		0x14,0x7F,0x14,0x7F,0x14,//#3
		0x00,0x4C,0x7A,0x4F,0x32,//$4
		0x00,0x66,0x16,0x68,0x66,//%5
		0x00,0x38,0x4F,0x4D,0x32,//&6
		0x00,0x00,0x00,0x03,0x00,//'7
		0x00,0x00,0x3E,0x41,0x00,//(8
		0x00,0x00,0x41,0x3E,0x00,//)9
		0x00,0x1C,0x3E,0x1C,0x00,//*10
		0x00,0x08,0x3E,0x08,0x00,//+11
		0x00,0x00,0x00,0x60,0x00,//,12
		0x00,0x08,0x08,0x08,0x08,//-13
		0x00,0x00,0x40,0x00,0x00,//.14
		0x00,0x40,0x30,0x0C,0x03,///15
		0x00,0x3E,0x41,0x41,0x3E,//016
		0x00,0x42,0x7F,0x40,0x00,//117
		0x00,0x62,0x51,0x49,0x46,//218
		0x00,0x22,0x49,0x49,0x36,//319
		0x00,0x38,0x26,0x7F,0x20,//420
		0x00,0x4F,0x49,0x49,0x31,//521
		0x00,0x3E,0x49,0x49,0x32,//622
		0x00,0x03,0x71,0x09,0x07,//723
		0x00,0x36,0x49,0x49,0x36,//824
		0x00,0x26,0x49,0x49,0x3E,//925
		0x00,0x00,0x24,0x00,0x00,//:26
		0x00,0x40,0x24,0x00,0x00,//;27
		0x00,0x08,0x14,0x22,0x41,//<28
		0x00,0x14,0x14,0x14,0x14,//=29
		0x00,0x41,0x22,0x14,0x08,//>30
		0x00,0x02,0x51,0x09,0x06,//?31
		0x00,0x0E,0x71,0x49,0x7E,//@32
		0x00,0x7E,0x11,0x11,0x7E,//A33
		0x00,0x7F,0x49,0x49,0x36,//B34
		0x00,0x3E,0x41,0x41,0x22,//C35
		0x00,0x7F,0x41,0x41,0x3E,//D36
		0x00,0x7F,0x49,0x49,0x41,//E37
		0x00,0x7F,0x09,0x09,0x01,//F38
		0x00,0x3E,0x41,0x49,0x3A,//G39
		0x00,0x7F,0x08,0x08,0x7F,//H40
		0x00,0x41,0x7F,0x41,0x00,//I41
		0x00,0x30,0x40,0x40,0x3F,//J42
		0x00,0x7F,0x08,0x14,0x63,//K43
		0x00,0x7F,0x40,0x40,0x40,//L44
		0x00,0x7F,0x06,0x06,0x7F,//M45
		0x00,0x7F,0x06,0x18,0x7F,//N46
		0x00,0x7F,0x41,0x41,0x7F,//O47
		0x00,0x7F,0x09,0x09,0x06,//P48
		0x00,0x3E,0x51,0x61,0x7E,//Q49
		0x00,0x7F,0x19,0x29,0x46,//R50
		0x00,0x26,0x49,0x49,0x32,//S51
		0x00,0x01,0x7F,0x01,0x01,//T52
		0x00,0x3F,0x40,0x40,0x3F,//U53
		0x00,0x0F,0x70,0x70,0x0F,//V54
		0x00,0x7F,0x30,0x30,0x7F,//W55
		0x00,0x63,0x1C,0x1C,0x63,//X56
		0x03,0x04,0x78,0x04,0x03,//Y57
		0x00,0x61,0x59,0x4D,0x43,//Z58
		0x00,0x00,0x7F,0x41,0x00,//[59
		0x03,0x0C,0x30,0x40,0x00,//\60
		0x00,0x00,0x41,0x7F,0x00,//]61
		0x00,0x02,0x01,0x02,0x00,//^62
		0x00,0x40,0x40,0x40,0x40,//_63
		0x00,0x00,0x03,0x00,0x00,//`64
		0x00,0x20,0x58,0x58,0x78,//a65
		0x00,0x7F,0x48,0x48,0x30,//b66
		0x00,0x30,0x48,0x48,0x48,//c67
		0x00,0x30,0x48,0x48,0x7F,//d68
		0x00,0x30,0x58,0x58,0x10,//e69
		0x00,0x08,0x7C,0x0A,0x08,//f70
		0x00,0x10,0xA8,0xA8,0x78,//g71
		0x00,0x7F,0x08,0x78,0x00,//h72
		0x00,0x00,0x74,0x00,0x00,//i73
		0x00,0x80,0x80,0x74,0x00,//j74
		0x00,0x7F,0x10,0x28,0x48,//k75
		0x00,0x40,0x7F,0x40,0x00,//l76
		0x00,0x78,0x08,0x78,0x78,//m77
		0x00,0x78,0x08,0x08,0x70,//n78
		0x00,0x30,0x48,0x48,0x30,//o79
		0x00,0xF8,0x48,0x48,0x30,//p80
		0x00,0x30,0x48,0x48,0xF8,//q81
		0x00,0x78,0x10,0x08,0x08,//r82
		0x00,0x50,0x58,0x68,0x28,//s83
		0x00,0x08,0x7C,0x48,0x00,//t84
		0x00,0x38,0x40,0x40,0x78,//u85
		0x00,0x18,0x60,0x60,0x18,//v86
		0x18,0x60,0x38,0x60,0x18,//w87
		0x00,0x48,0x30,0x30,0x48,//x88
		0x00,0x18,0xA0,0xA0,0x78,//y89
		0x00,0x48,0x68,0x58,0x48,//z90
		0x00,0x08,0x77,0x41,0x00,//{91
		0x00,0x00,0x7F,0x00,0x00,//|92
		0x00,0x41,0x77,0x08,0x00,//}93
		0x00,0x02,0x01,0x02,0x01,//~94
		0x00,0x02,0x51,0x09,0x06 //?95
	};
}
//*******************************************************
//AVR特殊寄存器定义
unit sfr
{
	//端口
	public uint8 PORTA = @0x3b;
	public uint8 DDRA = @0x3a;
	public uint8 PINA = @0x39;
	
	public uint8 PORTB = @0x38;
	public uint8 DDRB = @0x37;
	public uint8 PINB = @0x36;
	
	public uint8 PORTC = @0x35;
	public uint8 DDRC = @0x34;
	public uint8 PINC = @0x33;
	
	public uint8 PORTD = @0x32;
	public uint8 DDRD = @0x31;
	public uint8 PIND = @0x30;
	
	//AD转换控制
	public uint8 ADCL = @0x24;
	public uint8 ADCH = @0x25;
	public uint8 ADCSRA = @0x26;
	public uint8 ADMUX = @0x27;
	
	//UART串口控制
	public uint8 UCSRB = @0x2a;
	public uint8 UCSRA = @0x2b;
	public uint8 UCSRC = @0x40;
	public uint8 UBRRL = @0x29;
	public uint8 UBRRH = @0x40;
	public uint8 UDR = @0x2c;
	
	//定时/计数器2
	public uint8 ASSR = @0x42;
	public uint8 OCR2 = @0x43;
	public uint8 TCNT2 = @0x44;
	public uint8 TCCR2 = @0x45;
	
	//定时/计数器0
	public uint8 TCNT0 = @0x52;
	public uint8 TCCR0 = @0x53;
	
	//外部中断控制
	public uint8 MCUCSR = @0x54;
	public uint8 MCUCR = @0x55;
	public uint8 TIMSK = @0x59;
	public uint8 GIFR = @0x5a;
	public uint8 GICR = @0x5b;
	
	//堆栈指针
	public uint8 SP_LOW = @0x5d;
	public uint8 SP_HIGH = @0x5e;
	public uint16 SP = @0x5d;
	
	//状态寄存器
	public uint8 SREG = @0x5f;
	
	public uint8 TWBR = @0x00;
	public uint8 TWSR = @0x01;
	public uint8 TWAR = @0x02;
	public uint8 TWDR = @0x03;
	public uint8 ACSR = @0x08;
	public uint8 SPCR = @0x0d;
	public uint8 SPSR = @0x0e;
	public uint8 SPDR = @0x0f;
	public uint8 EECR = @0x1c;
	public uint8 EEDR = @0x1d;
	public uint8 EEARL = @0x1e;
	public uint8 EEARH = @0x1f;
	public uint16 EEAR = @0x1e;   // 16 bit
	public uint8 WDTCR = @0x21;
	public uint8 ICR1L = @0x26;
	public uint8 ICR1H = @0x27;
	public uint8 OCR1BL = @0x28;
	public uint8 OCR1BH = @0x29;
	public uint16 OCR1B = @0x28;  // 16 bit
	public uint8 OCR1AL = @0x2a;
	public uint8 OCR1AH = @0x2b;
	public uint16 OCR1A = @0x2a;  // 16 bit
	public uint8 TCNT1L = @0x2c;
	public uint8 TCNT1H = @0x2d;
	public uint16 TCNT1 = @0x2c;  // 16 bit
	public uint8 TCCR1B = @0x2e;
	public uint8 TCCR1A = @0x2f;
	public uint8 SFIOR = @0x30;
	public uint8 OSCCAL = @0x31;
	public uint8 TWCR = @0x36;
	public uint8 SPMCR = @0x37;
	public uint8 TIFR = @0x38;
	public uint8 OCR0 = @0X3c;
}





⌨️ 快捷键说明

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