📄 iodemo.c
字号:
#include <pic18.h>
#include "iodemo.h"
void interrupt ISR(void);
void init(void);
void update_ports(void);
/* This is the test pattern array */
const unsigned char LEDDISPLAY[]={0xF8,0xF1,0xE3,0xC7,0x8F,0x1F,0x3E,0x7C,0xF8,0xF1,0xE3,0xC7,0x8F,0x1F,0x3E,0x7C};
unsigned char A;
void main(void)
{
init();
/* After the devices are initialised, the main loop just polls timer 1. */
/* Every time timer 1 overflows, the ports will give an update to the */
/* LED panels. */
while(1)
{
if(TMR1IF)
{
TMR1IF=0;
TMR1H=0xF0; /* Timer reload, adjusts the speed of LED updates */
update_ports();
}
}
}
void init(void)
{
GIE=1;
IPEN=0;
T1CON=0x81; /* Timer 1 needs to be activated */
TMR1IE=0; /* No interrupts are associated with timer 1 */
ADCON1=0x0F; /* Set relevant port pins to digital mode, rather than analog */
TRISA=0; /* initially ports will be in output mode to display test pattern */
TRISB=0xFF; /* PORTB must be set as input so that the interrupts are possible */
TRISC=0;
TRISD=0;
TRISE=0;
TRISF=0;
TRISG=0;
TRISH=0;
TRISJ=0;
TRISK=0;
RBPU=0;
RBIE=1; /* enable PORTB interrupts */
RBIF=0;
}
void update_ports(void)
{
static unsigned char index;
index++; /* used to adavnce the test pattern */
index&=0x07;
PORTA=LEDDISPLAY[index]; /* load test pattern to the ports */
PORTC=LEDDISPLAY[index+1];
PORTD=LEDDISPLAY[index+2];
PORTE=LEDDISPLAY[index+3];
PORTF=LEDDISPLAY[index+4];
PORTG=LEDDISPLAY[index+5];
PORTH=LEDDISPLAY[index+6];
PORTJ=LEDDISPLAY[index+7];
PORTK=LEDDISPLAY[index+3];
}
void interrupt ISR(void)
{
unsigned char direction;
if((RBIE)&&(RBIF))
{
direction=TRISC; /* save PORTC status */
TRISC=0xFF; /* now make PORTC all input to read the DIP switch */
switch((PORTB)&0xF0) /* read PORTB, which port has been selected? */
{
case PORTASELECTED:
TRISA=PORTC; /* assign new direction to that port */
break;
case PORTCSELECTED:
direction=PORTC;
break;
case PORTDSELECTED:
TRISD=PORTC;
break;
case PORTESELECTED:
TRISE=PORTC;
break;
case PORTFSELECTED:
TRISF=PORTC;
break;
case PORTGSELECTED:
TRISG=PORTC;
break;
case PORTHSELECTED:
TRISH=PORTC;
break;
case PORTJKSELECTED:
TRISJ=(PORTC&0x0F); /* PORTJ & K share and LED panel */
TRISK=((PORTC>>4)&0x0F);
break;
}
TRISC=direction; /* restore PORTC status */
RBIF=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -