📄 main.c
字号:
/*************************************************************************
*
* Used with ICCARM and AARM.
*
* (c) Copyright IAR Systems 2006
*
* File name : main.c
* Description : Main module
*
* History :
* 1. Date : 2, December 2006
* Author : Stanimir Bonev
* Description : Create
*
* This example project shows how to use the IAR Embedded Workbench for ARM
* to develop code for the IAR LPC-2378-SK board. It shows basic use of the I/O,
* the timer, the interrupt controllers and the LDC module for graphic and text
* drawing.
* It starts by show the IAR logo on the LCD and after 5 second draw a test screen.
*
* Jumpers:
* PWR_SEL - depending of power source
* RST_E - unfilled
* ISP_E - unfilled
*
* AN_TRIM - contrast / backlight
* BUT1 - adjust backlight
* BUT2 - adjust LCD contrast
*
* Note:
* After power-up the controller get clock from internal RC oscillator that
* is unstable and may fail with J-Link auto detect, therefore adaptive clocking
* should always be used. The adaptive clock can be select from menu:
* Project->Options..., section Debugger->J-Link/J-Trace JTAG Speed - Adaptive.
*
* $Revision: 1.0 $
**************************************************************************/
#include "includes.h"
#define TIMER0_TICK_PER_SEC 20
extern FontType_t Terminal_6_8_6;
extern FontType_t Terminal_9_12_6;
extern FontType_t Terminal_18_24_12;
volatile Boolean CntrSel = FALSE;
/*************************************************************************
* Function Name: fiq_handler
* Parameters: none
*
* Return: none
*
* Description: FIQ handler
*
*************************************************************************/
__fiq __arm void FIQ_Handler (void)
{
while(1);
}
/*************************************************************************
* Function Name: irq_handler
* Parameters: none
*
* Return: none
*
* Description: IRQ handler
*
*************************************************************************/
__irq __arm void IRQ_Handler (void)
{
void (*interrupt_function)();
unsigned int vector;
vector = VICADDRESS; // Get interrupt vector.
interrupt_function = (void(*)())vector;
if(interrupt_function != NULL)
{
interrupt_function(); // Call vectored interrupt function.
}
else
{
VICADDRESS = 0; // Clear interrupt in VIC.
}
}
/*************************************************************************
* Function Name: Timer0IntrHandler
* Parameters: none
*
* Return: none
*
* Description: Timer 0 interrupt handler
*
*************************************************************************/
void Timer0IntrHandler (void)
{
if(!(B1_FIO & B1_MASK))
{
CntrSel = FALSE;
}
else if (!(B2_FIO & B2_MASK))
{
CntrSel = TRUE;
}
// clear interrupt
T0IR_bit.MR0INT = 1;
VICADDRESS = 0;
}
/*************************************************************************
* Function Name: VIC_Init
* Parameters: void
* Return: void
*
* Description: Initialize VIC
*
*************************************************************************/
void VIC_Init(void)
{
volatile unsigned long * pVecAdd, *pVecCntl;
int i;
// Assign all interrupt channels to IRQ
VICINTSELECT = 0;
// Disable all interrupts
VICINTENCLEAR = 0xFFFFFFFF;
// Clear all software interrupts
VICSOFTINTCLEAR = 0xFFFFFFFF;
// VIC registers can be accessed in User or privileged mode
VICPROTECTION = 0;
// Clear interrupt
VICADDRESS = 0;
// Clear address of the Interrupt Service routine (ISR) for vectored IRQs
// and disable all vectored IRQ slots
for(i = 0, pVecCntl = &VICVECTPRIORITY0, pVecAdd = &VICVECTADDR0; i < 32; ++i)
{
*pVecCntl++ = *pVecAdd++ = 0;
}
}
/*************************************************************************
* Function Name: VIC_SetVectoredIRQ
* Parameters: void(*pIRQSub)()
* unsigned int VicIrqSlot
* unsigned int VicIntSouce
*
* Return: void
*
* Description: Init vectored interrupts
*
*************************************************************************/
void VIC_SetVectoredIRQ(void(*pIRQSub)(), unsigned int Priority,
unsigned int VicIntSource)
{
unsigned long volatile *pReg;
// load base address of vectored address registers
pReg = &VICVECTADDR0;
// Set Address of callback function to corresponding Slot
*(pReg+VicIntSource) = (unsigned long)pIRQSub;
// load base address of ctrl registers
pReg = &VICVECTPRIORITY0;
// Set source channel and enable the slot
*(pReg+VicIntSource) = Priority;
// Clear FIQ select bit
VICINTSELECT &= ~(1<<VicIntSource);
}
/*************************************************************************
* Function Name: InitClock
* Parameters: void
* Return: void
*
* Description: Initialize PLL and clocks' dividers. Hclk - 288MHz,
* Usbclk - 48MHz
*
*************************************************************************/
void InitClock(void)
{
// 1. Init OSC
SCS_bit.OSCRANGE = 0;
SCS_bit.OSCEN = 1;
// 2. Wait for OSC ready
while(!SCS_bit.OSCSTAT);
// 3. Disconnect PLL
PLLCON_bit.PLLC = 0;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 4. Disable PLL
PLLCON_bit.PLLE = 0;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 5. Select source clock for PLL
CLKSRCSEL_bit.CLKSRC = 1; // Selects the main oscillator as a PLL clock source.
// 6. Set PLL settings 288 MHz
PLLCFG_bit.MSEL = 24-1;
PLLCFG_bit.NSEL = 2-1;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 7. Enable PLL
PLLCON_bit.PLLE = 1;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 8. Wait for the PLL to achieve lock
while(!PLLSTAT_bit.PLOCK);
// 9. Set clk divider settings
CCLKCFG = 6-1; // 1/6 Fpll
USBCLKCFG = 6-1; // 1/6 Fpll - 48 MHz
PCLKSEL0 = PCLKSEL1 = 0; // other peripherals
// 10. Connect the PLL
PLLCON_bit.PLLC = 1;
PLLFEED = 0xAA;
PLLFEED = 0x55;
}
/*************************************************************************
* Function Name: GpioInit
* Parameters: void
* Return: void
*
* Description: Reset all GPIO pins to default: primary function
*
*************************************************************************/
void GpioInit(void)
{
// Set to inputs
IO0DIR = \
IO1DIR = \
FIO0DIR = \
FIO1DIR = \
FIO2DIR = \
FIO3DIR = \
FIO4DIR = 0;
SCS_bit.GPIOM = 1; // enable fast GPIOs
// clear mask registers
FIO0MASK =\
FIO1MASK =\
FIO2MASK =\
FIO3MASK =\
FIO4MASK = 0;
// Reset all GPIO pins to default primary function
PINSEL0 =\
PINSEL1 =\
PINSEL2 =\
PINSEL3 =\
PINSEL4 =\
PINSEL5 =\
PINSEL6 =\
PINSEL7 =\
PINSEL8 =\
PINSEL9 =\
PINSEL10= 0;
}
/*************************************************************************
* Function Name: SYS_GetFsclk
* Parameters: none
* Return: Int32U
*
* Description: return Sclk [Hz]
*
*************************************************************************/
Int32U SYS_GetFsclk(void)
{
Int32U Mul = 1, Div = 1, Osc, Fsclk;
if(PLLSTAT_bit.PLLC)
{
// when PLL is connected
Mul = PLLSTAT_bit.MSEL + 1;
Div = PLLSTAT_bit.NSEL + 1;
}
// Find clk source
switch(CLKSRCSEL_bit.CLKSRC)
{
case 0:
Osc = I_RC_OSC_FREQ;
break;
case 1:
Osc = MAIN_OSC_FREQ;
break;
case 2:
Osc = RTC_OSC_FREQ;
break;
default:
Osc = 0;
}
// Calculate system frequency
Fsclk = Osc*Mul*2;
Fsclk /= Div*(CCLKCFG+1);
return(Fsclk);
}
/*************************************************************************
* Function Name: SYS_GetFpclk
* Parameters: Int32U Periphery
* Return: Int32U
*
* Description: return Pclk [Hz]
*
*************************************************************************/
Int32U SYS_GetFpclk(Int32U Periphery)
{
Int32U Fpclk;
pInt32U pReg = (pInt32U)((Periphery < 32)?&PCLKSEL0:&PCLKSEL1);
Periphery &= 0x1F; // %32
Fpclk = SYS_GetFsclk();
// find peripheral appropriate periphery divider
switch((*pReg >> Periphery) & 3)
{
case 0:
Fpclk /= 4;
break;
case 1:
break;
case 2:
Fpclk /= 2;
break;
default:
Fpclk /= 8;
}
return(Fpclk);
}
/*************************************************************************
* Function Name: ClearFlag
* Parameters: void
* Return: void
*
* Description: clear arg
*
*************************************************************************/
void ClearFlag (void* arg)
{
int* pFlag = arg;
*pFlag = 0;
}
/*************************************************************************
* Function Name: Dly100us
* Parameters: void *arg
* Return: void
*
* Description: Delay [100us]
*
*************************************************************************/
void Dly100us(void *arg)
{
volatile Int32U Dly = (Int32U)arg, Dly100;
for(;Dly;Dly--)
for(Dly100 = 500; Dly100; Dly100--);
}
/*************************************************************************
* Function Name: main
* Parameters: none
*
* Return: none
*
* Description: main
*
*************************************************************************/
void DrawTable (void)
{
Int32U x,y,j,k;
// xxxxRRRRGGGGBBBB
const Int32U TableColor [] =
{
0xFFF, 0xAAA, 0x888, 0x666, 0x444, 0x000, // Grey
0x00F, 0x00A, 0x008, 0x006, 0x004, 0x001, // Red
0x0F0, 0x0A0, 0x080, 0x060, 0x040, 0x010, // Green
0xF00, 0xA00, 0x800, 0x600, 0x400, 0x100, // Blue
};
for(y = 0; y < 4; ++y)
{
for(x = 0; x < 6; ++x)
{
// set rectangle
k = (x*22) | ((((x+1)*22)-1)<<8);
GLCD_SendCmd(CASET,(pInt8U)&k,0);
k = (y*29) | ((((y+1)*29)-1)<<8);
GLCD_SendCmd(RASET,(pInt8U)&k,0);
// fill rectangle
for(j = 0; j < (22*29); ++j)
{
GLCD_SendCmd(RAMWR,(pInt8U)&TableColor[x+(y*6)],2);
}
}
}
}
/*************************************************************************
* Function Name: main
* Parameters: none
*
* Return: none
*
* Description: main
*
*************************************************************************/
int main(void)
{
Boolean SelHold;
Int32U AdcData;
// MAM init
MAMCR_bit.MODECTRL = 0;
MAMTIM_bit.CYCLES = 3; // FCLK > 40 MHz
MAMCR_bit.MODECTRL = 2; // MAM functions fully enabled
// Init clock
InitClock();
// Init GPIO
GpioInit();
// Init VIC
VIC_Init();
// But 0,1 init
B1_FDIR &= ~B1_MASK;
B2_FDIR &= ~B2_MASK;
// ADC Init
// Assign P1.31 to AIN5
PINSEL3_bit.P1_31 = 3;
PCONP_bit.PCAD = 1; // Enable ADC clk
// Set ADC clk <4.5 MHz
AD0CR_bit.CLKDIV = (SYS_GetFpclk(24) / 45000000) + 1;
// Select AIN5
AD0CR_bit.SEL = 1<<5;
// Disable all interrupts
ADINTEN = 0;
// Enable ADC
AD0CR_bit.PDN = 1;
// Start conversion
AD0CR_bit.START = 1;
// Init Time0
PCONP_bit.PCTIM0 = 1; // Enable TMR0 clk
T0TCR_bit.CE = 0; // counting disable
T0TCR_bit.CR = 1; // set reset
T0TCR_bit.CR = 0; // release reset
T0CTCR_bit.CTM = 0; // Timer Mode: every rising PCLK edge
T0MCR_bit.MR0I = 1; // Enable Interrupt on MR0
T0MCR_bit.MR0R = 1; // Enable reset on MR0
T0MCR_bit.MR0S = 0; // Disable stop on MR0
// set timer 0 period
T0PR = 0;
T0MR0 = SYS_GetFpclk(TIMER0_PCLK_OFFSET)/(TIMER0_TICK_PER_SEC);
// init timer 0 interrupt
T0IR_bit.MR0INT = 1; // clear pending interrupt
VIC_SetVectoredIRQ(Timer0IntrHandler,0,VIC_TIMER0);
VICINTENABLE |= 1UL << VIC_TIMER0;
T0TCR_bit.CE = 1; // counting Enable
__enable_interrupt();
// GLCD init
#ifdef IAR_KICKSTART
GLCD_PowerUpInit(NULL);
GLCD_SetFont(&Terminal_9_12_6,0x000F00,0x00FF0);
GLCD_SetWindow(16,40,131,131);
GLCD_TextSetPos(0,0);
printf(" IAR Embedded\n\r"
"Workbench for ARM\r\n"
" Kickstart\r\n");
#else
GLCD_PowerUpInit((pInt8U)IAR_Logo.pPicStream);
#endif
LcdSetBacklight(BACKLIGHT_ON);
Dly100us((void*)30000);
DrawTable();
GLCD_SetFont(&Terminal_9_12_6,0x000F00,0x00FF0);
GLCD_SetWindow(10,116,131,131);
GLCD_TextSetPos(0,0);
if(CntrSel)
{
SelHold = TRUE;
printf("\fContrast adj.\r");
}
else
{
SelHold = FALSE;
printf("\fBacklight adj.\r");
}
while(1)
{
AdcData = AD0GDR;
if(AdcData & (1UL << 31))
{
// Start conversion
AD0CR_bit.START = 1;
AdcData >>= 10;
AdcData &= 0xFF;
if(SelHold)
{
// Contract adj
GLCD_SendCmd(SETCON,(pInt8U)&AdcData,0);
}
else
{
// Backlight adj
AdcData >>= 1;
LcdSetBacklight(AdcData);
}
}
if(SelHold != CntrSel)
{
SelHold ^= 1;
if(SelHold)
{
printf("\fContrast adj.\r");
}
else
{
printf("\fBacklight adj.\r");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -