/*
EM6819 toggle example
This is the simplest example. It just toggles Port A.
Use it for your first steps of using the EM6819 toolchain.
Power the target board, connect the RLink, make the project (F9)
and then program the application. (click "Tool"->"Run Tool..."->"EM6819_pgm")
You will then see the LEDs connected on Port A toggling
according to the pattern defined in the tables.
*/
#define MAIN_FILE 1
#include "EM6819\periph.h"
#include "EM6819\macro.h"
#include "EM6819\Types.h"
//choose which timer we'll be using and define a few things accordingly
#define TIMER_NB 3
#if TIMER_NB == 1
#define Handle_Irq_TimX Handle_Irq_Tim1
#define TimXSWStart Tim1SWStart
#define RegTimXCfg RegTim1Cfg
#define RegTimXCfgVal ( Tim1SelClk_2 | Tim1SelClk_1 | Tim1SelClk_0 )
#define RegTimXFull RegTim1Full
#define RegIntYMsk RegInt0Msk
#define IntYMskTimX Int0MskTim1
#endif
#if TIMER_NB == 2
#define Handle_Irq_TimX Handle_Irq_Tim2
#define TimXSWStart Tim2SWStart
#define RegTimXCfg RegTim2Cfg
#define RegTimXCfgVal ( Tim2SelClk_2 | Tim2SelClk_1 )
#define RegTimXFull RegTim2Full
#define RegIntYMsk RegInt1Msk
#define IntYMskTimX Int1MskTim2
#endif
#if TIMER_NB == 3
#define Handle_Irq_TimX Handle_Irq_Tim3
#define TimXSWStart Tim3SWStart
#define RegTimXCfg RegTim3Cfg
#define RegTimXCfgVal ( Tim3SelClk_2 | Tim3SelClk_1 )
#define RegTimXFull RegTim3Full
#define RegIntYMsk RegInt1Msk
#define IntYMskTimX Int1MskTim3
#endif
#if TIMER_NB == 4
#define Handle_Irq_TimX Handle_Irq_Tim4
#define TimXSWStart Tim4SWStart
#define RegTimXCfg RegTim4Cfg
#define RegTimXCfgVal ( Tim4SelClk_2 | Tim4SelClk_0 )
#define RegTimXFull RegTim4Full
#define RegIntYMsk RegInt2Msk
#define IntYMskTimX Int2MskTim4
#endif
// Function section
void donothing(){;}
//these tables define the pattern of the toggling
//this is NOT the most efficient way to write this program,
// but it shows how to set data in the different sections.
//section rodata
const unsigned char consttable[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
//section data
unsigned char vartable[] = {0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF};
//this is the index for the tables above
volatile unsigned char i=0;
//timer tick counter reload value
//the length of the delay, which determines the frequency of the toggle,
// is directly proportionnal to this value
#define RELOAD_delay_irq_cnt (0x1F)
//timer tick counter
//the timer IRQ decrements this counter
volatile unsigned int delay_irq_cnt=RELOAD_delay_irq_cnt;
//timer ISR
void Handle_Irq_TimX(void)
{// Handles the interruption from Timer, Input capture, Compare value, Compare Full
//decrement counter and check if it is zero
if(!(delay_irq_cnt--))
{
//reload counter init value
delay_irq_cnt=RELOAD_delay_irq_cnt;
//increment table index and check if need to restart
i++;
if(i>=(sizeof(consttable)+sizeof(vartable)))
{
//_breakpoint();
i=0;
Nop;
//_breakpoint();
Nop;
Nop;
}
}
//restart timer
RegTimersStart = TimXSWStart;
}
//and now, the main function...
int main(void)
{
unsigned char table[20];
unsigned char *ptrdst;
const unsigned char *ptrsrc;
//disable watchdog
RegWDKey = 0xCA ;
RegWDCfg = 0x80 ;
//copy data from initialized tables to work table
ptrdst=table;
ptrsrc=consttable;
for(i=0; i<sizeof(consttable); i++)
*(ptrdst++) = *(ptrsrc++);
ptrsrc=vartable;
for(i=0; i<sizeof(vartable); i++)
*(ptrdst++) = *(ptrsrc++);
//configure PA as output
RegPAOE = 0xFF;
//turn all PA signals ON
RegPADOut = 0xFF;
//Set timer clock source
//the frequency also depends on this.
//see the device datasheet for more information.
RegTimXCfg = RegTimXCfgVal;
//timer reload value
//determines the time between two timer ticks
//the length of the delay, which determines the frequency of the toggle,
// is directly proportionnal to this value
RegTimXFull = 0xFF;
//enable timer IRQ.
RegIntYMsk |= IntYMskTimX;
//start timer
RegTimersStart = TimXSWStart;
//main loop. display the content of the table
while (1)
{
RegPADOut = ~table[i];
Nop;
Halt;
Nop;
}
return 0;
}
//uncomment this for filling the flash (for prog speed tests...)
//const unsigned char bigconstempty[0x2000] = {0x00};
评论0