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

📄 remote._c

📁 硬盘MP3播放器-MEGA128-VS1001K-USB-遥控-LCD-ICCAVR代码-硬盘-支持FAT32-单面PCB 第二部分
💻 _C
字号:
// Check Remote hardware version in Remote.h
// Add french accent

#include <iom8v.h>
#include <macros.h>
#include <string.h>
#include <stdio.h>
#include "NokiaLCD.h"
#include "remote.h"
#include "eeprom.h"

//#define NOTIMEOUT

//******************************************************************
//*	MAIN Global Variable
//******************************************************************
unsigned int TimeOut = 0;
unsigned char Key = NONE;
char Line[5][MAXLEN];
unsigned char RxData[MAXLEN*2];
signed char LinePtr = 0;
unsigned char ListType = SONG;
unsigned char NewMessage = FALSE;
unsigned char Refresh = FALSE;
unsigned int Light = 0; 
unsigned char Cont = 25;

unsigned char UpdateLcd;


unsigned char buffer[10];
//*************************************
// void main(void)
//*************************************
void main(void)
{
// PortB is setup by LcdInit() 
// PB1 is share for LCD and 38khz oscilator
// PB6..7 is for external Xtal

// PortC is for KeyPad and set as input
DDRC = 0x00;        // PC as input
PORTC = 0xff;       // Pull up on PC pin	

// PortD is for Serial TX&RX, and Int0 input
DDRD = 0x18;        // All input exept PD4 output VRX, PD3 output LIGHT
PORTD = 0xe4;       // Pull up on all PD exept TX & RX & LIGHT

// Int0 is for Keypress interrupt done via diode on each key 
MCUCR = 0x02;       // int0 on falling eage level
GICR = 0x40;        // int0 enable

// Timer 0
TCCR0 = 0x04;       // Timer1 / 256

// Timer 1
TCCR1B = 0x09;		// Timer 1 prescaler at /1
OCR1A  = 0x17;		// for 38khz

// Serial
UCSRA = 0x00;  		// Clear flags, single speed, No multi processor
UCSRC = 0x8e;  		// Asynch, No parity, 2 stopbit, 8bit
UBRRL = 47;    		// 2400bps at 1.8432Mhz
UBRRH = 0x00;  
UCSRB = 0x98;  		// RX & TX Enable

Line[0][0] = 0x00;
Line[1][0] = 0x00;
Line[2][0] = 0x00;
Line[3][0] = 0x00;
Line[4][0] = 0x00;

TIMSK = 0x01;       // Timer0 overflow interrupt enable

LcdInit();
EEPROM_READ(0x10, Cont);	
LcdContrast(Cont);
Presentation();
LineDisplay();
SEI();

PORTD |= VRX;

while(1)
		{
		_StackCheck();

		if (TimeOut <= 10000)
		   {
		   if (NewMessage == TRUE)
   		   	  {
			  CLI();
		   	  AnalyseData();
		   	  NewMessage = FALSE;
			  SEI();
   		   	  }
 
		   if (Light > 1000) 
		   	  {
		   	  PORTD &= ~LIGHT;
		   	  Light = 1001;
		   	  }

		   if (Key != NONE)
		   	  {
			  TimeOut = 0;
			  Light = 0;
			  PORTD |= LIGHT;
			  
			  if (Key == RESET) 
			  	 {
				 CLI();
				 TIMSK = 0x00;
				 UCSRB = 0x18;  		// RX & TX Enable
				 asm("jmp 0x0000");
				 }
				 
			  if (Key == DOWN)
			  	 {
				 SendKey(0x01);
				 }
				 
			  if (Key == SHIFT_DOWN)
			  	 {
				 SendKey(0x81);
				 }			 
				 
			  if (Key == UP)
			  	 {
				 SendKey(0x02);
				 }
				 
			  if (Key == SHIFT_UP)
			  	 {
				 SendKey(0x82);
				 }
				 
			  if (Key == LEFT)
			  	 {
				 SendKey(0x04);
				 } 
				 
			  if (Key == RIGHT)
			  	 {
				 SendKey(0x08);
				 }	
				 
			  if (Key == SHIFT_RIGHT)
			  	 {
				 Cont++;
				 if (Cont > 0x7f) Cont = 0;
				 LcdContrast(Cont);
				 EEPROM_WRITE(0x10, Cont);	
				 }
				 
			  if (Key == SHIFT_LEFT)
			  	 {
				 Cont--;
				 if (Cont == 0x00) Cont = 0x7f;
				 LcdContrast(Cont);
				 EEPROM_WRITE(0x10, Cont);	
				 }				 
				 
			  Key = NONE;	 
		   	  }
		   }
		else
		   {
		   // Go in DEEP SLEEP
		   LcdClear();
		   LcdPower(FALSE);
		   PORTD &= ~VRX;     // Disable Receiver
		   // Disable Timer0 overflow because the LCD must be init first 
		   // after wake up
		   TIMSK &= ~0x01;

		   MCUCR = 0xa0;      // Power Down MCU and int0 low level
		   SLEEP();

		   TimeOut = 0;
		   LcdPower(TRUE);
		   LcdContrast(Cont);
		   PORTD |= VRX;      // Enable Receiver
		   Presentation();
		   LineDisplay();
		   TIMSK |= 0x01;  	  // Timer0 overflow interrupt enable
		   MCUCR = 0x02;  	  // int0 on falling eage level
		   }
		}
}

//*************************************
// void _StackOverflowed(char c)
//*************************************
void _StackOverflowed(char c)
	{
	CLI();
	LcdClear();
	LcdGotoXY(1,3);
	LcdStrConst(1,"Stack Crash...\0");
	LcdUpdate();
	while(1);
	}

//*************************************
// void Presentation(void)
//*************************************
void Presentation(void)
{
unsigned char i;

LcdClear();
LcdGotoXY(3,1);
LcdStrConst(1,"MP3 Player" );
for (i=0;i<9;i++) LcdLine(0,i,83,i,PIXEL_XOR);
LcdGotoXY(5,3);
LcdStrConst(1,"Sylvain");
LcdGotoXY(3,4);
LcdStrConst(1,"Bissonnette");
LcdGotoXY(4,6);
LcdStrConst(1,"Ver : 1.0");
LcdUpdate();
delay_ms(1000);
LcdClear();
LcdUpdate();
}

//*************************************
// void LineDisplay(void)
//*************************************
void LineDisplay(void)
{
unsigned char i;
char buffer[14];

LcdClear();
LcdGotoXY(3,1);
if (ListType == SONG) LcdStrConst(1,"Song List" );
if (ListType == DIR)  LcdStrConst(1,"Dir  List" );
if (ListType == PLAY) LcdStrConst(1," Playing" );
for (i=1;i<8;i++) LcdLine(0,i,83,i,PIXEL_XOR);

for (i=0;i<5;i++)
	{
	memcpy(&buffer[0],&Line[i][0],13);
	buffer[13] = 0x00;
	LcdGotoXY(2,2+i);
	LcdStr(1,&buffer[0]);
	}
LcdGotoXY(1,2+LinePtr);
LcdChr(1,134); // c'est la fleche
}

//*************************************
// void timer1_ovf_isr(void)
// 
// 3.8 Hit/Second
//*************************************
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{

static unsigned char tick = 0;
static unsigned char i = 0;
static unsigned char LastLinePtr = 0;
static char buffer[14];
unsigned char j;

if (LinePtr != LastLinePtr) // if line ptr change
   {
   LineDisplay();
   i = 0;
   LastLinePtr = LinePtr;
   }

if (Refresh == TRUE)
   {
   LineDisplay();
   i = 0;
   Refresh = FALSE;
   }

if (UpdateLcd == TRUE) LcdUpdate();
   
tick++;
if (tick < 6) return;
tick = 0;
   
#ifndef NOTIMEOUT
TimeOut++;
#endif   
Light++;


if (strlen(&Line[LinePtr][0]) < 14) return;
  
for (j=0;j<14;j++) buffer[j] = ' ';
if (i > (strlen(&Line[LinePtr][0]))) i = 0;
j = strlen(&Line[LinePtr][i]);
if (j > 14) j = 14;
memcpy(&buffer[0],&Line[LinePtr][i],j);
buffer[13] = 0x00;
LcdGotoXY(2,2+LinePtr);
LcdStr(1,&buffer[0]);
i++;
}

//*************************************
// void int0_isr(void)
//*************************************
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
static unsigned char LastKey;
unsigned char i;

TimeOut = 0;

delay_ms(10);
Key = ScanKey();
}

/**********************************************************

Name:			void ScanKey(void)

Description:	ScanKeyPad

Input:			none

Output:			none

Misc:			

**********************************************************/	
unsigned char ScanKey()
{
if ((!(PINC & SHIFT)) && (!(PINC & UP)) && (!(PINC & DOWN))) return RESET;

if ((!(PINC & SHIFT)) && (!(PINC & UP)))    return SHIFT_UP;
if ((!(PINC & SHIFT)) && (!(PINC & DOWN)))  return SHIFT_DOWN;
if ((!(PINC & SHIFT)) && (!(PINC & RIGHT))) return SHIFT_RIGHT;
if ((!(PINC & SHIFT)) && (!(PINC & LEFT)))  return SHIFT_LEFT;

if (!(PINC & UP))     return UP;
if (!(PINC & DOWN))   return DOWN;
if (!(PINC & RIGHT))  return RIGHT;
if (!(PINC & LEFT))   return LEFT;

return NONE;
}

//******************************************************************
//*	Delay millisecond Function
//*   
//*   Clock = 1Mhz
//******************************************************************
void delay_ms(int ms)
{
int i,j;

for (j=0;j<ms;j++) 
	{
	for (i=0;i<130;i++);
	WDR();
	}
}

//*************************************
// void uart0_rx_isr(void)  
//
// manchester encoded byte
//*************************************
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
unsigned char ch;
unsigned char i;
static unsigned char Error;
static unsigned char Decoded;
static unsigned char Qte;

ch = UDR;  //uart has received a character in UDR

if (ch == 0xf0) 
   {
   Qte = 0;
   Error = FALSE;
   return;
   }
   
if ((ch == 0x0f) && (Error == FALSE)) 
   {
   RxData[Qte/2] = 0x00;
   NewMessage = TRUE;
   return;
   }

Decoded = 0x00;

for (i=0;i<4;i++)
	{
	Decoded >>= 1;
	if ((ch & 0x03) == 0x02) Decoded |= 0x08;
	else if ((ch & 0x03) == 0x01) Decoded = Decoded;
	else Error = TRUE;
	ch >>= 2;
	}

if (Qte <= (MAXLEN * 2)) 
   {
   if ((Qte % 2) == 0) RxData[Qte/2] = Decoded;
   else RxData[Qte/2] += (Decoded << 4);
   }
Qte++;
}

//*************************************
// void AnalyseData(void)
//*************************************
void AnalyseData(void)
{
unsigned char i;

TIMSK &= ~0x01;		   // Timer0 overflow interrupt disable
if (RxData[0] == 0x80) // Clear Screen
   {
   for (i=0;i<14;i++)
	  	  {
	  	  Line[0][i] = 0x00;
	  	  Line[1][i] = 0x00;
	  	  Line[2][i] = 0x00;
	  	  Line[3][i] = 0x00;
	  	  Line[4][i] = 0x00;
		  }
   }
   
if (RxData[0] == 0x81) strncpy(&Line[0][0],&RxData[1],MAXLEN);
if (RxData[0] == 0x82) strncpy(&Line[1][0],&RxData[1],MAXLEN);
if (RxData[0] == 0x83) strncpy(&Line[2][0],&RxData[1],MAXLEN);
if (RxData[0] == 0x84) strncpy(&Line[3][0],&RxData[1],MAXLEN);
if (RxData[0] == 0x85) strncpy(&Line[4][0],&RxData[1],MAXLEN);

if (RxData[0] == 0x90) ListType = SONG;
if (RxData[0] == 0x91) ListType = DIR;
if (RxData[0] == 0x92) ListType = PLAY;

if ((RxData[0] > 0xa0) && (RxData[0] < 0xa6)) LinePtr = RxData[0] - 0xa1;

if (RxData[0] == 0xb0) Refresh = TRUE;

TIMSK |= 0x01;  	  // Timer0 overflow interrupt enable
}

//*************************************
// void Send(unsigned char ch)
//*************************************
void SendKey(unsigned char ch)
{
unsigned char i;

UCSRB &= ~0x80;  		// Disable RX interrupt
TxChar(0x55);			// Setup AGC
TxChar(0x55);			// Setup AGC
TxChar(0xf0);			// Start of Frame
TxChar(ch);				// Send value
TxChar(~ch);			// Send value
TxChar(0x0f);			// End of Frame

i = UDR;  				// Clear uart received character 
UCSRB |= 0x80;			// Enable RX interrupt
}

//*************************************
// void TxChar(unsigned char ch)
//*************************************
void TxChar(unsigned char ch)
{
TIMSK &= ~0x01;
while (!(UCSRA & 0x20));      // Wait for empty transmit buffer
TCCR1A = 0x40; 	 			  // Enable 38khz
UDR = ch;	  		   		  // Write char
while (!(UCSRA & 0x40));      // Wait for transmit finish
UCSRA = 0x40;  	 			  // Clear transmit finish flag
TCCR1A = 0x00;				  // Disable 38khz
TIMSK |= 0x01;
}

⌨️ 快捷键说明

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