📄 r591io.c
字号:
/*======================================================================*/
/* NAME: r591io.c */
/* INFO: In/Out module for the PHYTEC training board equipped with */
/* PHYTEC phyCORE 591 and Philips P8x591 Microcontroller */
/* RIGHTS: Embedded Systems Academy www.esacademy.com */
/*----------------------------------------------------------------------*/
/* DETAILS: LED D1 can be dimmed using set_brightness_d1 - this only */
/* works after the call of init_timer0_dim_d1. Timer 0 is used */
/* to create a PWM signal to D1 */
/*----------------------------------------------------------------------*/
/* HISTORY: V1.0 Pf 21-FEB-2000 */
/* V1.1 Pf 16-JUN-2000 */
/*======================================================================*/
#include "reg591.h"
#include "reg591ex.h"
/*------------------------- GLOBAL DATA --------------------------------*/
WORD timer1tick;
/*-------------------------- LOCAL DATA --------------------------------*/
BYTE D1InUse = 0; /* D1 used to display analog value? */
WORD T0VALON, T0VALOFF; /* reload values for ON and OFF */
WORD t1reload; /* reload for timer 1 */
BYTE statusD1; /* LED 1=ON, 0=OFF */
char xdata *pPD; /* Pointer to Address of ext. Port D */
/*----------------------- GLOBAL SUBROUTINES ---------------------------*/
/*======================================================================*/
/* FUNCTION: check_button */
/* DESCRIPTION:Reading the status of push button S1 or S2 */
/* INPUT: Sx: 1 or 2 for reading button S1 or S2 */
/* OUTPUT: 0, if button SX is not pushed down */
/* 0xFF, if button is pushed down */
/*======================================================================*/
BYTE check_button (BYTE Sx)
{
BYTE button; /* Define and init vars */
pPD = ADDR_INPUT_PORT; /* Set pointer to external port address */
button = *pPD & Sx; /* read external port */
if (button == 0)
return (0);
else
return (0xFF);
}
/*======================================================================*/
/* FUNCTION: read_dip_switches */
/* DESCRIPTION:Read the settings of the 8 DIP switches S5 */
/* INPUT: none */
/* OUTPUT: Current value of the DIP switches S5 */
/*======================================================================*/
BYTE read_dip_switches (void)
{
char xdata *loc_pPD; /* Pointer to Address of ext. Port D */
loc_pPD = ADDR_DIP_INPUT_PORT; /* Set pointer to external port address */
return (*loc_pPD);
}
/*======================================================================*/
/* FUNCTION: read_poti */
/* DESCRIPTION:Read an analog input value at P1.2 - Potentiometer */
/* CAUTION: Starts a A/D conversion and WAITS until it is completed */
/* INPUT: none */
/* OUTPUT: Current value of the DIP switches S5 */
/*======================================================================*/
BYTE read_poti (void)
{
int poti;
ADCON &= 0xF8; /* select POTI on P1.2 */
ADCON |= 0x08; /* set ADCS to start conversion */
while(!(ADCON == (ADCON | 0x10))); /* wait for int flag */
ADCON &= 0xEF; /* clear int flag */
poti = (0xFF - ADCH);
return (poti); /* return 0-100 */
}
/*======================================================================*/
/* FUNCTION: switch_leds */
/* DESCRIPTION:Switches LED D1 and D2 on or off */
/* Will not use LED D1 if init_timer0_dim_d1 was called */
/* INPUT: on_off: 0 switches both LEDs off, */
/* 1 switches LED D1 on, D2 off */
/* 2 switches LED D1 off, D2 on */
/* 3 switches both LEDs on */
/* OUTPUT: none */
/*======================================================================*/
void switch_leds (BYTE on_off)
{
pPD = ADDR_INPUT_PORT; /* Set pointer to external port address. */
if (D1InUse)
{
on_off &= 2;
if (on_off > 0)
*pPD |= 0x20;
else
*pPD &= 0xDF;
}
else
*pPD = (on_off << 4); /* write to external port */
}
/*======================================================================*/
/* FUNCTION: init_timer1 */
/* DESCRIPTION:Initializes timer 1 in mode 3 */
/* Increments the global variable timer1tick with each */
/* occurance of the timer 1 interrupt */
/* CAUTION: EA needs to be set to 1 manually after this function is */
/* called to globally enable ALL interrupts */
/* INPUT: reload: the 16-bit reload value for the time */
/*----------------------------------------------------------------------*/
/* To get specific reload values calculated (for example to achieve a */
/* 5ms interrupt), use the Microcontroller Peripheral Timing Calculator */
/* provided by the Embedded Systems Academy at */
/* www.esacademy.com/faq/calc/philips.htm */
/*----------------------------------------------------------------------*/
/* OUTPUT: none */
/*======================================================================*/
void init_timer1 (WORD reload)
{
t1reload = reload;
timer1tick = 0;
TR1 = 0; /* timer 1: stop */
TMOD |= 0x10; /* mode 1 */
IP1 |= 2; /* priority */
TH1 = reload / 256;
TL1 = reload % 256;
TR1 = 1; /* timer 1: start */
ET1 = 1; /* enable timer 1 int */
}
/*======================================================================*/
/* FUNCTION: init_timer0_dim_d1 */
/* DESCRIPTION:Initializes timer 0 which is used to dim D1 */
/* Needs to be called before set_brightness_d1 can be used */
/* CAUTION: EA needs to be set to 1 manually after this function is */
/* called to globally enable ALL interrupts */
/* INPUT: none */
/* OUTPUT: none */
/*======================================================================*/
void init_timer0_dim_d1 (void)
{
D1InUse = 0xFF; /* D1 is in use */
T0VALON = 0xFFC0; /* reload for ON */
T0VALOFF = 0xD000; /* reload for OFF */
statusD1 = 0; /* used to toggle LED */
pPD = ADDR_INPUT_PORT; /* initialize write address for LEDs */
TR0 = 0; /* timer 0: stop */
TMOD |= 1; /* mode 1 */
TH0 = 0xE0;
TL0 = 0x00;
TR0 = 1; /* timer 0: start */
ET0 = 1; /* enable timer 0 int */
}
/*======================================================================*/
/* FUNCTION: set_brightness_d1 */
/* DESCRIPTION:Sets the brightness of LED D1 */
/* CAUTION: init_timer0_dim_d1 needs to be called before this */
/* function can work */
/* INPUT: 0-100, a value of 100 or higher makes LED D1 brightest */
/* OUTPUT: none */
/*======================================================================*/
void set_brightness_d1 (BYTE value)
{
if (value > 100)
value = 100;
T0VALON = 0xFFD0 - (value * value);
T0VALOFF = 0xCC00 + (value * 128);
}
/*======================================================================*/
/* FUNCTION: error_state */
/* DESCRIPTION:If a program discovers a fatal error, this routine can */
/* be used to display an error code to the user. */
/* THIS FUNCTION NEVER RETURNS! ONLY A RESET WILL RECOVER */
/* THE BOARD!
/* INPUT: Error value to be displayed on both LEDs as a blinking */
/* pattern. Only values from 1-12 can be "reasonably" */
/* recognized by counting the "blinks" */
/* OUTPUT: none */
/*======================================================================*/
void error_state (BYTE error)
{
WORD i,j;
EA = 0; /* Disable all interrupts */
while (1)
{
for (j = 1; j <= error; j++)
{
for (i = 0; i <= 20000; i++)
switch_leds(0x03); /* Both LEDs on */
for (i = 0; i <= 20000; i++)
switch_leds(0x00); /* Both LEDs off */
}
for (i = 0; i <= 50000; i++)
switch_leds(0x00); /* Both LEDs off */
}
}
/*----------------------- LOCAL SUBROUTINES ----------------------------*/
/*======================================================================*/
/* FUNCTION: t0_isr */
/* DESCRIPTION:Interrupt Service Routine for timer 0 */
/* Controls the brightness of LED d1 */
/* INPUT: T0VALON and T0VALOFF - current reload values for timer */
/* OUTPUT: none */
/*======================================================================*/
void t0_isr (void) interrupt 1
{
TR0 = 0; /* timer 0: stop */
if (statusD1==0) /* LED is OFF */
{
*pPD = *pPD | 0x10; /* switch D1 on */
TH0 = T0VALON / 256;
TL0 = T0VALON % 256; /* reload value for on */
statusD1 = 1;
}
else
{
*pPD = *pPD & 0xEF; /* switch D1 off */
TH0 = T0VALOFF / 256; /* reload value for off */
TL0 = T0VALOFF % 256;
statusD1 = 0;
}
TR0 = 1; /* timer 0: restart */
}
/*======================================================================*/
/* FUNCTION: t1_isr */
/* DESCRIPTION:Interrupt Service Routine for timer 1 */
/* Increments the variable timer1tick with each occurance */
/* INPUT: t1reload - reload value set by function init_timer1 */
/* OUTPUT: none */
/*======================================================================*/
void t1isr (void) interrupt 3
{
TR1 = 0; /* timer 1: stop */
TH1 = t1reload / 256;
TL1 = t1reload % 256; /* reload value */
TR1 = 1; /* timer 1: restart */
timer1tick++;
}
/*----------------------- END OF FILE ----------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -