PIC 18F452
PROGRAMMING IN’C’
1)USART
#include <stdio.h>
#pragma config WDT = OFF
void main (void)
{
printf ("Helen of troy!\n");
while (1);
}
2)ADC
#include<p18f452.h>
void MSDelay(unsigned int);
void main(void)
{
TRISAbits.TRISA0=1;
TRISC=0;
TRISD=0;
ADCON0=0x81;
ADCON1=0xCE;
while(1)
{
MSDelay(1);
ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);
PORTD=ADRESL;
PORTC=ADRES
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<150;j++);
}
3)CAPTURE CCP
#include<p18f452.h>
void main(void)
{
unsigned char I;
CCP1CON=0x04;
TRISD=0x00;
TRISCbits.TRISC2=1;
T3CON=0x80;
T1CON=0x81;
PIE1=0x04;
while(PIR1!=0x04);
{
I=TMR1L;
PORTD=I;
PIR1=0x00;
}
}
4)CHANGEpbINTERUPT
#include<p18f452.h>
#define LED1 PORTCbits.RC4
#define LED2 PORTCbits.RC5
void chk_isr(void);
void RBINT_ISR(void);
void MSDelay(unsigned int);
#pragma code My_HiPrio_Int=0x0008
void My_HiPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
#pragma interrupt chk_isr
void chk_isr(void)
{
if(INTCONbits.RBIF==1)
RBINT_ISR();
}
void main(void)
{
TRISD=0x00;
TRISCbits.TRISC4=0;
TRISCbits.TRISC5=0;
TRISBbits.TRISB4=1;
TRISBbits.TRISB5=1;
INTCONbits.RBIF=0;
INTCONbits.RBIE=1;
INTCONbits.GIE=1;
PORTD=0x55;
while(1);
}
void RBINT_ISR(void)
{
LED1=PORTBbits.RB4;
PORTD=0;
LED2=PORTBbits.RB5;
MSDelay(200);
PORTD=0xFF;
MSDelay(2000);
PORTD=0;
MSDelay(2000);
PORTD=0xFF;
MSDelay(2000);
PORTD=0x00;
INTCONbits.RBIF=0;
}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<150;j++);
}
5)COMPARE CCP
#include<p18f452.h>
void main(void)
{
while(1)
{
CCP1CON=0x02;
T3CON=0x42;
TRISCbits.TRISC2=0;
TRISCbits.TRISC0=1;
CCPR1L=10;
CCPR1H=0;
while(1)
{
TMR3H=0;
TMR3L=0;
PIR1bits.CCP1IF=0;
T3CONbits.TMR3ON=1;
while(PIR1bits.CCP1IF==0);
T3CONbits.TMR3ON=0;
}
}
}
6)COMPAREmod
#include<p18f452.h>
void main(void)
{
CCP1CON=0x02;
CCPR1=0x80;
T1CON=0x00;
TRISCbits.TRISC2=0;
TMR1H=0x00;
TMR1L=0x00;
PIE1=0x04;
T1CON=0x81;
while(PIR1!=0x04);
PIR1=0x00;
}
7)COMPARETIMER1
#include<p18f452.h>
void main(void)
{
while(1)
{
CCP1CON=0x02;
T3CON=0x0;
T1CON=0x0;
TRISCbits.TRISC2=0;
TRISCbits.TRISC0=1;
CCPR1L=0x50;
CCPR1H=0xC3;
while(1)
{
TMR1H=0;
TMR3L=0;
PIR1bits.CCP1IF=0;
T3CONbits.TMR3ON=1;
while(PIR1bits.CCP1IF==0)
T3CONbits.TMR3ON=0;
INTCONbits.TMR0IF==0;
}
}
8)COUNTERt0
#include<p18f452.h>
void main(void)
{
TRISAbits.TRISA4=1;
TRISD=0;
T0CON=0x68;
TMR0L=0x00;
while(1)
{
do
{
T0CONbits.TMR0ON=1;
PORTD=TMR0L;
}
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF==0;
}
}
9)COUNTERt3
#include<p18f452.h>
void main(void)
{
TRISCbits.TRISC0=1;
TRISB=0;
TRISD=0;
T3CON=0x02;
TMR3H=0x00;
TMR3L=0x00;
while(1)
{
do
{
T3CONbits.TMR3ON=1;
PORTD=TMR3L;
PORTB=TMR3H;
}
while(PIR2bits.TMR3IF==0);
T3CONbits.TMR3ON=0;
PIR2bits.TMR3IF=0;
}}
10)extINTERRUPT
#include<p18f452.h>
#define mybit PORTBbits.RB4
void chk_isr(void);
void INT0_isr(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
if(INTCONbits.INT0IF==1)
INT0_isr();
}
#pragma code My_HiPrio_Int=0x08
void My_HiPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
void main(void)
{
TRISBbits.TRISB0=1;
TRISBbits.TRISB5=0;
TRISC=0xFF;
TRISD=0;
INTCONbits.INT0IF=0;
INTCONbits.INT0IE=1;
INTCONbits.GIE=1;
while(1)
{
PORTD=PORTC;
}
}
void INT0_isr(void)
{
mybit=~mybit;
INTCONbits.INT0IF=0;
}
11)Iomodule
#include <p18f452.h>
#include <delays.h>
void main( )
{
ADCON1 = 0x0F;
ADCON0bits.ADON = 0;
TRISA=0;
TRISB=0;
TRISC=0;
TRISD=0;
TRISE=0;
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;
INTCON=0x00;
INTCON2=0x00;
INTCON3=0x00;
while(1)
{
PORTA=0xC0;
PORTB=0xC0;
PORTC=0x00;
PORTD=0x00;
PORTE=0x00;
Delay10KTCYx(1);
PORTA=0xFF;
PORTB=0xFF;
PORTC=0xFF;
PORTD=0x55;
PORTE=0xFF;
//Delay10KTCYx(1);
}}
12)LCD
#include <p18f452.h>
#include <delays.h>
#define READ PORTEbits.RE0 /*READ=SET &
WRITE=RESET*/
#define STROBE PORTEbits.RE1 /*DATA
STROBE=(ENABLE)*/
#define CTRL PORTEbits.RE2
void InitLCD();
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void CursorHome();
void CursorSet( unsigned char pos);
void main()
{
static unsigned char t[]="WELCOME TO TRIOZTECH" ;
static int k;
ADCON1 = 0x0F;
ADCON0bits.ADON = 0;
TRISB=0;
PORTB=0;
TRISE=0;
PORTE=0;
TRISC=0;
PORTC=0;
InitLCD(); /* Initialize the LCD */
CursorHome(); /* Set the location of cursor at first position*/
k=0;
while(t[k])
{
if(k==24) //K=24 for 2X24 ,4 bit mode LCD
{
WriteCtrl(12); /* Write to control word */
Delay1KTCYx(50); //delay for 1.5ms
WriteCtrl(0); /* Move to next line */
Delay1KTCYx(50); //delay for 1.5ms;
}
WriteData(t[k]);
k++;
}
Delay10KTCYx(200);
while(1);
}
/*****************************************************
; Function: InitLCD
; Description: Initializes the LCD
; Inputs: none
; Returns: nothing
;****************************************************/
void InitLCD()
{
/* Wait a bit after power-up */
Delay1KTCYx(20000); //delay for 50ms
/* Initialize the LCD to 4-bit mode */
WriteCtrl(3);
Delay1KTCYx(10000); //delay for 25ms
WriteCtrl(3);
Delay1KTCYx(10000); //delay for 25ms
WriteCtrl(3);
Delay1KTCYx(10000);
WriteCtrl(2);
Delay1KTCYx(10000);
/*Function Set */
WriteCtrl(2);
Delay1KTCYx(10000);
WriteCtrl(8);
Delay1KTCYx(1000);
/* Display OFF */
WriteCtrl(0);
Delay1KTCYx(1000);
WriteCtrl(8);
Delay1KTCYx(1000);
/* Display ON */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(12);
Delay1KTCYx(1000);
/* Entry mode */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(4);
Delay1KTCYx(100);
/* Clear Screen */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(1);
Delay1KTCYx(100);
/* Cursor home */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(2);
Delay1KTCYx(200);
}
/*******************Cursoret() function ******************
* Function to locate the cursor at desired position
* INPUTS : POSITION of the cursor 'pos'
* OUTPUTS : None
*****************************************************/
void CursorSet( unsigned char pos )
{
WriteCtrl(8);
Delay1KTCYx(5000); //delay for 1.25ms
WriteCtrl(pos);
Delay1KTCYx(5000);
}
void CursorHome()
{
WriteCtrl(0);
Delay1KTCYx(5000); //delay for 1.25ms
WriteCtrl(2);
Delay1KTCYx(5000);
}
void WriteCtrl( unsigned char value )
{
CTRL=0;
WriteCommon( value );
}
void WriteData( unsigned char value )
{
CTRL=1;
WriteCommon( value > 4 );
WriteCommon( value );
}
void WriteCommon( unsigned char value )
{
READ=0;
value = value & 0x0F;
value = value < 4;
PORTC = PORTC & 0x0F;
PORTC = PORTC | value;
PORTB=PORTC;
PORTB = PORTB > 2;
STROBE=1;
Delay1KTCYx(5000); //delay for 1.25ms
STROBE=0;
READ=1;
Delay1KTCYx(5000); //delay for 1.25ms
}
13)LCDpro
#include <p18f452.h>
#define IDATA PORTB
#define rs PORTEbits.RE2
#define rw PORTEbits.RE0
#define en PORTEbits.RE1
void lcddata(unsigned char);
void lcdcmd(unsigned char);
void MSDelay(unsigned int);
void main()
{
TRISB=0;
TRISD=0;
en=0;
MSDelay(5);
lcdcmd(8);
MSDelay(5);
lcdcmd(3);lcdcmd(8);lcdcmd(8);
lcdcmd(0x38);
MSDelay(5);
lcdcmd(0x38);
MSDelay(5);
lcdcmd(0x0E);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);
lcddata('M');
MSDelay(15);
lcddata('I');
MSDelay(15);
lcddata('b');
MSDelay(15);
lcddata('i');
}
void lcdcmd(unsigned char value)
{
IDATA=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
}
void lcddata(unsigned char value)
{
IDATA=value;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}
14)LCDTEST2
#include <p18f452.h>
#include <delays.h>
#define READ PORTEbits.RE0 /*READ=SET &
WRITE=RESET*/
#define STROBE PORTEbits.RE1 /*DATA
STROBE=(ENABLE)*/
#define CTRL PORTEbits.RE2
void InitLCD();
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void CursorHome();
void CursorSet( unsigned char pos);
void main()
{
static unsigned char t[]="WELCOME TO TRIOZTECH" ;
static int k;
ADCON1 = 0x0F;
ADCON0bits.ADON = 0;
TRISB=0;
PORTB=0;
TRISE=0;
PORTE=0;
TRISC=0;
PORTC=0;
InitLCD(); /* Initialize the LCD */
CursorHome(); /* Set the location of cursor at first position*/
k=0;
while(t[k])
{
if(k==24) //K=24 for 2X24 ,4 bit mode LCD
{
WriteCtrl(12); /* Write to control word */
Delay1KTCYx(5); //delay for 1.5ms
WriteCtrl(0); /* Move to next line */
Delay1KTCYx(5); //delay for 1.5ms;
}
WriteData(t[k]);
k++;
}
Delay10KTCYx(20);
while(1);
}
/*****************************************************
; Function: InitLCD
; Description: Initializes the LCD
; Inputs: none
; Returns: nothing
;****************************************************/
void InitLCD()
{
/* Wait a bit after power-up */
Delay1KTCYx(200); //delay for 50ms
/* Initialize the LCD to 4-bit mode */
WriteCtrl(3);
Delay1KTCYx(100); //delay for 25ms
WriteCtrl(3);
Delay1KTCYx(100); //delay for 25ms
WriteCtrl(3);
Delay1KTCYx(100);
WriteCtrl(2);
Delay1KTCYx(100);
/*Function Set */
WriteCtrl(2);
Delay1KTCYx(100);
WriteCtrl(8);
Delay1KTCYx(100);
/* Display OFF */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(8);
Delay1KTCYx(100);
/* Display ON */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(12);
Delay1KTCYx(100);
/* Entry mode */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(4);
Delay1KTCYx(100);
/* Clear Screen */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(1);
Delay1KTCYx(100);
/* Cursor home */
WriteCtrl(0);
Delay1KTCYx(100);
WriteCtrl(2);
Delay1KTCYx(200);
}
/*******************Cursoret() function ******************
* Function to locate the cursor at desired position
* INPUTS : POSITION of the cursor 'pos'
* OUTPUTS : None
*****************************************************/
void CursorSet( unsigned char pos )
{
WriteCtrl(8);
Delay1KTCYx(5); //delay for 1.25ms
WriteCtrl(pos);
Delay1KTCYx(5);
}
void CursorHome()
{
WriteCtrl(0);
Delay1KTCYx(5); //delay for 1.25ms
WriteCtrl(2);
Delay1KTCYx(5);
}
void WriteCtrl( unsigned char value )
{
CTRL=0;
WriteCommon( value );
}
void WriteData( unsigned char value )
{
CTRL=1;
WriteCommon( value > 4 );
WriteCommon( value );
}
void WriteCommon( unsigned char value )
{
READ=0;
value = value & 0x0F;
value = value < 4;
PORTC = PORTC & 0x0F;
PORTC = PORTC | value;
PORTB=PORTC;
PORTB = PORTB > 2;
STROBE=1;
Delay1KTCYx(5); //delay for 1.25ms
STROBE=0;
READ=1;
Delay1KTCYx(5); //delay for 1.25ms
}
15)LED
void Stop();
#define INT8U unsigned char
/* BOOLEAN */
#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0
//
/* Error handling */
#define RETCODE int
#define ERRCODE int
#define SUCCESS 0
//
#define SBR_19200 9600 /* for MM51-C board */
//
#define SCL PORTCbits.RC3 /* I2C clock */
#define SDA PORTCbits.RC4 /* I2C data */
#define I2C_DEVICE_24CXX 0xA0
#define I2C_DEVICE_SAA1064 0x70
/* C0 */
#define STATIC 0x00
#define DYNAMIC 0x01
/* C1 */
#define BLANK_13 0x00
#define NOT_BLANK_13 0x02
/* C2 */
#define BLANK_24 0x00
#define NOT_BLANK_24 0x04
/* C3 */
#define WORK 0x00
#define TEST 0x08
/* C4 */
#define NO_CUR_3MA 0x00
#define CUR_3MA 0x10
/* C5 */
#define NO_CUR_6MA 0x00
#define CUR_6MA 0x20
/* C6 */
#define NO_CUR_12MA 0x00
#define CUR_12MA 0x40
#define A_SEG 0x20
#define B_SEG 0x10
#define C_SEG 0x04
#define D_SEG 0x02
#define E_SEG 0x01
#define F_SEG 0x40
#define G_SEG 0x80
#define DP_SEG 0x08
#define ZERO 0x77
#define ONE 0x14
#define TWO 0xb3
#define THREE 0xb6
#define FOUR 0xd4
#define FIVE 0xe6
#define SIX 0xe7
#define SEVEN 0x34
#define EIGHT 0xf7
#define NINE 0xf6
#define ALPHA_A 0xf5
#define ALPHA_B 0xc7
#define ALPHA_C 0x63
#define ALPHA_D 0x97
#define ALPHA_E 0xe3
#define ALPHA_F 0xe1
/*
#define ZERO A_SEG|B_SEG|C_SEG|D_SEG|E_SEG|F_SEG
#define ONE F_SEG|E_SEG
#define TWO A_SEG|B_SEG|G_SEG|E_SEG|D_SEG
#define THREE A_SEG|B_SEG|C_SEG|D_SEG|G_SEG
#define FOUR F_SEG|G_SEG|B_SEG|C_SEG
#define FIVE A_SEG|F_SEG|G_SEG|C_SEG|D_SEG
#define SIX A_SEG|F_SEG|E_SEG|D_SEG|C_SEG|G_SEG
#define SEVEN A_SEG|B_SEG|C_SEG
#define EIGHT A_SEG|B_SEG|C_SEG|D_SEG|E_SEG|F_SEG|G_SEG
#define NINE A_SEG|B_SEG|C_SEG|D_SEG|F_SEG|G_SEG
#define ALPHA_A A_SEG|B_SEG|C_SEG|E_SEG|F_SEG|G_SEG
#define ALPHA_B F_SEG|C_SEG|D_SEG|E_SEG|G_SEG
#define ALPHA_C A_SEG|F_SEG|E_SEG|D_SEG
#define ALPHA_D B_SEG|C_SEG|D_SEG|E_SEG|G_SEG
#define ALPHA_E A_SEG|F_SEG|E_SEG|D_SEG|G_SEG
#define ALPHA_F A_SEG|F_SEG|E_SEG|G_SEG
*/
unsigned char a,b,c,d;
/***********************************************************************
; Function: SendBuffer
;
; Description: sends LED_Command structure
; to the SAA1064
;
; Returns: - TRUE if the sending was good
; - FALSE if the sending was bad
;
*************************************************************************/
INT8U SendBuffer(INT8U Digit1,INT8U Digit2,INT8U Digit3,INT8U Digit4)
{
int a;
/*Start Transmission*/
Start();
/*slave addresss*/
Transmit(I2C_DEVICE_SAA1064);
/*instruction byte-control register*/
Transmit(0);
/*Control Byte*/
Transmit(DYNAMIC+NOT_BLANK_13+NOT_BLANK_24+WORK+CUR_12MA);
/*Digit 1*/
Transmit (Digit1);
/*Digit 2*/
Transmit (Digit2);
/*Digit 3*/
Transmit (Digit3);
/*Digit 4*/
Transmit (Digit4);
Stop();
return TRUE ;
}
/**************************************************************
; Function: Transmit
;
; Description: Transmits a byte to the I2C device with
; most significant bit first.
;
; Inputs: data - Contains the byte to be transmitted.
; SCL and SDA should be low on entry.
;
**************************************************************/
int Transmit(unsigned char data)
{
a = 0x80;
for(b=0;b<8;b++)
{
c = 0;
c = data & a;
if(c==0)
SDA=0;
else
SDA=1;
for(d=0;d<50;d++);
SCL=1;
for(d=0;d<50;d++);
SCL=0;
a = a>1;
}
SDA=1;
for(d=0;d<100;d++);
SCL=1;
for(d=0;d<100;d++);
SCL=0;
return 0;
}
/***************************************************************************
; Function: Start
;
; Description: Signals a Start which is defined as negative going transition
; of SDA while SCL is high.
;
; Inputs: None
;
; Returns: SCL and SDA are reset upon return.
; 1 - if bus is not available.
; 0 - if success.
***************************************************************************/
int Start(void)
{
SDA=1;
SCL=1;
for(a=0; a<100;a++);
SDA=0;
for(a=0; a<100;a++);
SCL=0;
return 0;
}
/*****************************************************************************
; Function: Stop
;
; Description: Signals a Stop which is defined as positive going transition
; of SDA while SCL is high.
;
; Inputs: SCL should be reset on entry.
;
; Returns: SCL and SDA are set upon return.
******************************************************************************/
void Stop(void)
{
SDA=0;
for(a=0; a<100;a++);
SCL=1;
for(a=0; a<100;a++);
SDA=1;
}
16)LED
#include <p18f452.h>
#include <delays.h>
#include "led.h"
void main()
{
PORTC = 0;
TRISC = 0;
Delay10KTCYx(1000);
while(1)
{
SendBuffer(ONE,TWO,THREE,FOUR);
Delay10KTCYx(1000);
SendBuffer(FIVE,SIX,SEVEN,EIGHT);
Delay10KTCYx(1000);
}}
17)LED BUZZER
#include <p18f452.h>
#include <delays.h>
void InterruptHandlerLow (void);
void main()
{
static int k; /* to be displayed */
PORTA = 0;
TRISA =0;
PORTC = 0;
TRISC =0;
/****Enable Interrupt*****/
PORTBbits.INT0=1;
TRISBbits.TRISB0=1;
INTCON=0x90;
ADCON1 = 0x06;
ADCON0bits.ADON = 0;
PORTA=0xff;
PORTC=0xff;
while(1)
{
for(k=0;k<=3;k++)
PORTA=0xFD; /* RA1=0 makes red LED to glow */
Delay10KTCYx(1000);
PORTC=0xFB; /* RC2=0 makes yellow LED to glow */
Delay10KTCYx(1000);
PORTC=0xFF;
PORTA=0xF7; /* RA3=0 makes green LED to glow */
Delay10KTCYx(1000);
}
for(k=0;k<=3;k++)
{
PORTA=0xFF;
PORTC=0xff;
Delay10KTCYx(1000);
PORTC=0xFB; /* RC2=0 makes yellow LED to glow */
PORTA=0xf5;
Delay10KTCYx(1000);
}
for(k=0;k<=3;k++)
{
PORTC=0xFB;
PORTA=0xFD; /* RA1=0 makes red LED to glow */
Delay10KTCYx(1000);
PORTA=0xF7; /* RA3=0 makes green LED to glow */
Delay10KTCYx(1000);
}
for(k=0;k<=3;k++)
{
PORTA=0xFF; /* RA1=0 makes red LED to glow */
PORTC=0xff;
Delay10KTCYx(1000);
PORTC=0xFB; /* RC2=0 makes yellow LED to glow */
PORTA=0xf5;
Delay10KTCYx(1000);
}
}
#pragma code InterruptVectorLow = 0x0018
void InterruptVectorLow( void )
{
_asm
goto InterruptHandlerLow
_endasm
}
#pragma code
#pragma interruptlow InterruptHandlerLow
void InterruptHandlerLow( void )
{
static int i,s;
INTCONbits.INT0IF=0;
for(i=0;i<75;i++)
{
for(s=10;s>0;s--)
{
PORTA=0xFE;
Delay1KTCYx(s);
PORTA=0xFF;
Delay1KTCYx(s);
}
}
}
18)LED TEST
#include <p18f452.h>
#include <delays.h>
#include "led.h"
main( )
{
PORTC = 0;
TRISC = 0;
Delay10KTCYx(1000);
while(1)
{
SendBuffer(ONE,TWO,THREE,FOUR);
Delay10KTCYx(1000);
SendBuffer(FIVE,SIX,SEVEN,EIGHT);
Delay10KTCYx(1000);
}
while(1);
}
19)PRTB
#include<P18f452.h>
void main(void)
{
TRISC=0;
for(;;)
{
PORTC=0x00;
}
}
20)PRTBITDELAY
#include<p18f452.h>
#define mybit PORTEbits.RE2
#define mybit1 PORTCbits.RC0
void MSDelay(unsigned int);
void main(void)
{
TRISEbits.TRISE2=0;
TRISCbits.TRISC0=0;
while(1)
{
mybit=1;
mybit1=0;
MSDelay(1000);
mybit=0;
mybit1=1;
MSDelay(1000);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<150;j++);
}
21)PRTDELAY
#include<p18f452.h>
void MSDelay(unsigned int);
void main(void)
{
TRISB=0;
TRISC=0;
while(1)
{
PORTC=0xFF;
PORTB=0x00;
MSDelay(2500);
PORTC=0x00;
PORTB=0xFF;
MSDelay(1000);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<150;j++);
}
22)PWM
#include<p18f452.h>
void main(void)
{
CCP1CON=0;
PR2=155;
CCPR1L=26;
TRISCbits.TRISC2=0;
T2CON=0x01;
CCP1CON=0x3C;
TMR2=0;
T2CONbits.TMR2ON=1;
while(1)
{
PIR1bits.TMR2IF=0;
while(PIR1bits.TMR2IF==0);
}}
23)pwmDCvary
#include<p18f452.h>
void main(void)
{
TRISCbits.TRISC2=0;
CCPR1L=0x40;
while(1)
{
PIR1=0x00;
PR2=0xFF;
CCP1CON=0x3C;
TMR2=0x00;
T2CON=0x04;
while(PIR1!=02);
CCPR1L=CCPR1L-10;
}}
24)READ PORT
#include<p18f452.h>
#define INbit PORTBbits.RB5
#define OUTbit PORTCbits.RC4
void main(void)
{
TRISBbits.TRISB5=1;
TRISCbits.TRISC4=0;
while(1)
{
OUTbit=INbit;
}
}
25)ROM
#include<p18f452.h>
rom const char mynum[]="0123456789";
void main(void)
{
unsigned char j;
TRISB=0;
for(j=0;j<10;j++)
PORTB=mynum[j];
}
26)ROMpragmaABCD
#include<p18f452.h>
#pragma code main=0x500
near rom const char mydata[]="0123456789,./';[]=-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void MSDelay(unsigned int);
void main(void)
{
unsigned char j;
TRISD=0;
for(j=0;j<45;j++)
{MSDelay(2500);
PORTD=mydata[j];
}}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<150;j++);
}
27)ROMwith DELAY
#include<p18f452.h>
void MSDelay(unsigned int);
rom const char mynum[]="0123456789";
void main(void)
{
unsigned char j;
TRISD=0;
for(j=0;j<10;j++)
{MSDelay(1000);
PORTD=mynum[j];
}}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<180;j++);
}
28)scalerTIMER
#include<p18f452.h>
#define mybit PORTBbits.RB1
void main(void)
{
TRISBbits.TRISB1=0;
T2CON=0x7F;
TMR2=0x00;
while(1)
{
PR2=0xFF;
T2CONbits.TMR2ON=1;
while(PIR1bits.TMR2IF==0);
mybit=~mybit;
T2CONbits.TMR2ON=0;
PIR1bits.TMR2IF=0;
}
}
29)SERHY