0% found this document useful (0 votes)
63 views22 pages

Code Final

The document contains 5 code examples that use timers, analog inputs, and LCD displays on a PIC microcontroller. The examples include blinking LEDs using a timer, adding two inputs, measuring temperature with an analog-to-digital converter and displaying on an LCD, scrolling text on an LCD controlled by a potentiometer, and implementing a digital clock that can be set using buttons. The code is written in C and uses libraries to initialize peripherals and components like timers, analog-to-digital conversion, and LCD displays.

Uploaded by

Khánh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views22 pages

Code Final

The document contains 5 code examples that use timers, analog inputs, and LCD displays on a PIC microcontroller. The examples include blinking LEDs using a timer, adding two inputs, measuring temperature with an analog-to-digital converter and displaying on an LCD, scrolling text on an LCD controlled by a potentiometer, and implementing a digital clock that can be set using buttons. The code is written in C and uses libraries to initialize peripherals and components like timers, analog-to-digital conversion, and LCD displays.

Uploaded by

Khánh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Bài 1:

#pragma config FNOSC = PRI


#pragma config POSCMOD = HS
#pragma config JTAGEN = OFF
#pragma config WINDIS = OFF
#pragma config FWDTEN = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"

int dem = 0;
volatile uint16_t Timer1_flag = 0;
void __attribute__ ((__interrupt__, auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0;
Timer1_flag = 1;
dem ++;
}

void Timer1_Init(uint16_t value)


{
T1CONbits.TON = 0;
T1CONbits.TCKPS = 0x03;
T1CONbits.TCS = 0;
PR1 = value;
IPC0bits.T1IP = 1;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;
}

void Timer1_Start()
{
T1CONbits.TON = 1;
}
int main(void)
{
uint16_t a = 0xA0;
uint16_t b = 0x01;
uint16_t c = 0x80;
TRISA = 0;
#ifdef __PIC24FJ1024GB610__
ANSA = 0;
#endif
Timer1_Init(15625);
Timer1_Start();
while(1)
{
if(Timer1_flag) //cau a
{
if(dem<25)
{
LATA = a;
a = ((a)>>1)|((a & 0x01)<<7);
}
if((dem>25)&&(dem<66)) //cau b
{
LATA = b + c;
b = ((b)<<1)|((b & 0x80)>>7);
c = ((c)>>1)|((c & 0x01)<<7);
}
Timer1_flag = 0;
}
if(dem>66)
{
dem = 0;
}
}
return 0;
}
Bài 2:
#pragma config FNOSC = PRI
#pragma config POSCMOD = HS
#pragma config JTAGEN = OFF
#pragma config WINDIS = OFF
#pragma config FWDTEN = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"

int main(void)
{
int a = 0, b = 0, cong = 0, S = 0;
TRISDbits.TRISD6 = 1;
TRISDbits.TRISD7 = 1;
TRISDbits.TRISD13 = 1;
#ifdef __PIC24FJ1024GB610__
ANSDbits.ANSD6 = 0;
ANSDbits.ANSD7 = 0;
#endif
TRISA = 0;
#ifdef __PIC24FJ1024GB610__
ANSA = 0;
#endif
while(1)
{
if(!PORTDbits.RD6)
{
while(!PORTDbits.RD6);
a++;
LATA = a;
__delay_ms(100);
}
if(!PORTDbits.RD7)
{
while(!PORTDbits.RD7);
b++;
LATA = b;
__delay_ms(100);
}
if(!PORTDbits.RD13)
{
LATA = 0;
while(!PORTDbits.RD13);
cong++;
__delay_ms(100);
}
switch(cong)
{
case 1:
S = a + b;
__delay_ms(100);
break;
case 2:
LATA = S;
__delay_ms(100);
break;
}
}
return 0;
}
Bài 3:
#pragma config FNOSC = PRI
#pragma config POSCMOD = HS
#pragma config JTAGEN = OFF
#pragma config WINDIS = OFF
#pragma config FWDTEN = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"
#include "lcd.h"

void analogInit()
{
#ifdef __PIC24FJ1024GB610__
AD1CON2bits.PVCFG = 0x0;
#else
AD1CON2bits.VCFG = 0x0;
#endif
AD1CON3bits.ADCS = 0xFF;
AD1CON1bits.SSRC = 0x0;
AD1CON3bits.SAMC = 0b10000;
AD1CON1bits.FORM = 0b00;
AD1CON2bits.SMPI = 0x0;
AD1CON1bits.ADON = 1;
}
uint16_t analogRead(int kenh)
{
uint16_t i;
switch(kenh)
{
case 4:
#ifdef __PIC24FJ1024GB610__
ANSBbits.ANSB4 = 1;
#else
AD1PCFGbits.PCFG4 = 0;
#endif
break;
case 5:
#ifdef __PIC24FJ1024GB610__
ANSBbits.ANSB5 = 1;
#else
AD1PCFGbits.PCFG5 = 0;
#endif
break;
}
AD1CHS = kenh;
AD1CON1bits.SAMP = 1;
for(i=0;i<1000;i++)
Nop();
AD1CON1bits.SAMP = 0;
for(i=0;i<1000;i++)
Nop();
while(!AD1CON1bits.DONE);
return ADC1BUF0;
}

int main(void)
{
uint16_t adc;
TRISA = 0;
#ifdef __PIC24FJ1024GB610__
ANSA = 0;
#endif
LCD_Initialize();
analogInit();
float t;
while(1)
{
adc = analogRead(4);
t = (100.0*3.3*adc/1024.0-50.0);
if(t<40)
{
LCD_ClearScreen();
printf("T = %.1f do C\r\n", t);
__delay_ms(500);
}
if(t>40)
{
while(1)
{
adc = analogRead(4);
t = (100.0*3.3*adc/1024.0-50.0);
LATA = 0;
__delay_ms(1000);
LATA = 0x07;
__delay_ms(1000);
LCD_ClearScreen();
printf("T = %.1f do C\r\n",t);
printf("LTK is kute\r");
__delay_ms(1000);
if(t<40)
break;
}
}
}
return 0;
}
Bài 4:
#pragma config FNOSC = PRI
#pragma config POSCMOD = HS
#pragma config JTAGEN = OFF
#pragma config WINDIS = OFF
#pragma config FWDTEN = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"
#include "lcd.h"

void analogInit()
{
#ifdef __PIC24FJ1024GB610__
AD1CON2bits.PVCFG = 0x0;
#else
AD1CON2bits.VCFG = 0x0;
#endif
AD1CON3bits.ADCS = 0xFF;
AD1CON1bits.SSRC = 0x0;
AD1CON3bits.SAMC = 0b10000;
AD1CON1bits.FORM = 0b00;
AD1CON2bits.SMPI = 0x0;
AD1CON1bits.ADON = 1;
}

uint16_t analogRead(int kenh)


{
uint16_t i;
switch(kenh)
{
case 4:
#ifdef __PIC24FJ1024GB610__
ANSBbits.ANSB4 = 1;
#else
AD1PCFGbits.PCFG4 = 0;
#endif
break;
case 5:
#ifdef __PIC24FJ1024GB610__
ANSBbits.ANSB5 = 1;
#else
AD1PCFGbits.PCFG5 = 0;
#endif
break;
}
AD1CHS = kenh;
AD1CON1bits.SAMP = 1;
for(i=0;i<1000;i++)
Nop();
AD1CON1bits.SAMP = 0;
for(i=0;i<1000;i++)
Nop();
while(!AD1CON1bits.DONE);
return ADC1BUF0;
}

int main(void)
{
uint16_t adc;
TRISA = 0;
#ifdef __PIC24FJ1024GB610__
ANSA = 0;
#endif
int16_t i = 0;
float R;
char a[22] = "LTK is super kute ";
analogInit();
LCD_Initialize();
while(1)
{
adc = analogRead(5);
R = (adc*10/1024.0);
printf("R = %.1f\r\n",R);
__delay_ms(600-50*R);
LCD_ClearScreen();
for(i=0;i<16;i++)
printf("%c", a[i]);
char temp = a[0];
i = 0;
while(i<22)
{
a[i] = a[i+1];
i++;
}
a[21] = temp;
}
return 0;
}
Bài 5:
#pragma config FNOSC = PRI
#pragma config POSCMOD = HS
#pragma config JTAGEN = OFF
#pragma config FWDTEN = OFF
#pragma config WINDIS = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"
#include "lcd.h"

volatile uint16_t Timer_flag = 0;


void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0;
Timer_flag = 1;
}
void Timer_Init(uint16_t value)
{
T1CONbits.TON = 0;
T1CONbits.TCKPS = 0x03;
T1CONbits.TCS = 0;
PR1 = value;
IPC0bits.T1IP = 1;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;
}
void Timer_Start()
{
T1CONbits.TON = 1;
}
int main(void)
{
uint16_t s;
int sec = 0, min = 0, h = 0, mode = 0;
LCD_Initialize();
TRISDbits.TRISD6 = 1;
TRISDbits.TRISD7 = 1;
TRISDbits.TRISD13 = 1;
TRISA = 0;
#ifdef __PIC24FJ1024GB610__
ANSDbits.ANSD6 = 0;
ANSDbits.ANSD7 = 0;
ANSA = 0;
#endif
Timer_Init(15625);
Timer_Start();
while(1)
{
if(Timer_flag)
{
s++;
sec = s;
if(sec==60)
{
s=0;
sec=0;
min++;
}
if(min==60)
{
min=0;
h++;
}
if(h==24)
{
h=0;
}
LCD_ClearScreen();
printf("%02d:%02d:%02d\r\n",h,min,sec);
Timer_flag=0;
}
if(!PORTDbits.RD13)
{
while(!PORTDbits.RD13);
mode++;
__delay_ms(100);
}
switch(mode)
{
case 1:
if(!PORTDbits.RD6)
{
while(!PORTDbits.RD6);
h++;
if(h>23)
h=0;
LCD_ClearScreen();
printf("%02d:%02d:%02d\r\n",h,min,sec);
}
if(!PORTDbits.RD7)
{
while(!PORTDbits.RD7);
h--;
if(h<0)
h=23;
LCD_ClearScreen();
printf("%02d:%02d:%02d\r\n",h,min,sec);
}
break;
case 2:
if(!PORTDbits.RD6)
{
while(!PORTDbits.RD6);
min++;
if(min>59)
min=0;
LCD_ClearScreen();
printf("%02d:%02d:%02d\r\n",h,min,sec);
}
if(!PORTDbits.RD7)
{
while(!PORTDbits.RD7);
min--;
if(min<0)
min=59;
LCD_ClearScreen();
printf("%02d:%02d:%02d\r\n",h,min,sec);
}
break;
case 3:
if(Timer_flag)
{
s++;
sec = s;
if(sec==60)
{
s=0;
sec=0;
min++;
}
if(min==60)
{
min=0;
h++;
}
if(h==24)
{
h=0;
}
LCD_ClearScreen();
printf("%02d:%02d:%02d\r\n",h,min,sec);
Timer_flag = 0;
mode = 0;
}
break;
}
}
return 0;
}
Bài 6:
#pragma config FNOSC = PRI
#pragma config POSCMOD = HS
#pragma config FWDTEN = OFF
#pragma config WINDIS = OFF
#pragma config JTAGEN = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"
#include "lcd.h"

volatile uint16_t Timer_flag = 0;


volatile uint16_t ms = 0;
void __attribute__ ((__interrupt__, auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0;
Timer_flag = 1;
ms++;
}

void Timer_Init(uint16_t value)


{
T1CONbits.TON = 0;
T1CONbits.TCKPS = 0x00;
T1CONbits.TCS = 0;
PR1 = value;
IPC0bits.T1IP = 1;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;
T1CONbits.TON = 1;
}
int main(void)
{
int msec, doi = 0;
TRISDbits.TRISD6 = 1;
TRISAbits.TRISA7 = 1;
TRISDbits.TRISD13 = 1;
#ifdef __PIC24FJ1024GB610__
ANSDbits.ANSD6 = 0;
ANSAbits.ANSA7 = 0;
#endif
LCD_Initialize();
Timer_Init(500);
while(1)
{
if(!PORTDbits.RD6)
{
while(!PORTDbits.RD6);
T1CONbits.TON = 1;
while(1)
{
if(Timer_flag)
{
msec = ms;
LCD_ClearScreen();
printf("Time = %.3f s",msec/1000.0);
Timer_flag = 0;
}
if(!PORTDbits.RD6)
{
while(!PORTDbits.RD6);
T1CONbits.TON = 0;
break;
}
}
}
if(!PORTDbits.RD13)
{
while(!PORTDbits.RD13);
__delay_ms(100);
if(doi == 0)
{
LCD_ClearScreen();
printf("Time = %d ms", msec);
doi = 1;
}
else
{
LCD_ClearScreen();
printf("Time = %.3f s",msec/1000.0);
doi = 0;
}
}

if(!PORTAbits.RA7)
{
while(!PORTAbits.RA7);
__delay_ms(100);
LCD_ClearScreen();
ms = 0;
msec = 0;
}
}
return 0;
}
Bài 7:
#pragma config POSCMOD = HS
#pragma config FNOSC = PRI
#pragma config WINDIS = OFF
#pragma config FWDTEN = OFF
#pragma config JTAGEN = OFF

#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"

volatile uint16_t Timer_flag = 0;


volatile uint16_t ms = 0;
void __attribute__ ((__interrupt__, auto_psv))_T1Interrupt(void)
{
IFS0bits.T1IF = 0;
Timer_flag = 1;
ms++;
}
void Timer_Init(uint16_t value)
{
T1CONbits.TON = 0;
T1CONbits.TCKPS = 0x01;
T1CONbits.TCS = 0;
PR1 = value;
IPC0bits.T1IP = 1;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;
T1CONbits.TON = 1;
}
void analogInit()
{
#ifdef __PIC24FJ1024GB610__
AD1CON2bits.PVCFG = 0x0;
#else
AD1CON2bits.VCFG = 0x0;
#endif
AD1CON3bits.ADCS = 0xFF;
AD1CON1bits.SSRC = 0x0;
AD1CON3bits.SAMC = 0b10000;
AD1CON1bits.FORM = 0b00;
AD1CON2bits.SMPI = 0x0;
AD1CON1bits.ADON = 1;
}

uint16_t analogRead(int kenh)


{
uint16_t i;
switch(kenh)
{
case 4:
#ifdef __PIC24FJ1024GB610__
ANSBbits.ANSB4 = 1;
#else
AD1PCFGbits.PCFG4 = 0;
#endif
break;
case 5:
#ifdef __PIC24FJ1024GB610__
ANSBbits.ANSB5 = 1;
#else
AD1PCFGbits.PCFG5 = 0;
#endif
break;
}
AD1CHS = kenh;
AD1CON1bits.SAMP = 1;
for(i=0;i<1000;i++)
Nop();
AD1CON1bits.SAMP = 0;
for(i=0;i<1000;i++)
Nop();
while(!AD1CON1bits.DONE);
return ADC1BUF0;
}

int main(void)
{
uint16_t adc;
int nhan = 0;
float R;
TRISDbits.TRISD13;
TRISDbits.TRISD6;
#ifdef __PIC24FJ1024GB610__
ANSDbits.ANSD6 = 0;
#endif
TRISA = 0;
#ifdef __PIC24FJ1024GB610__
ANSA = 0;
#endif
analogInit();
while(1)
{
if(!PORTDbits.RD13)
{
while(!PORTDbits.RD13);
__delay_ms(100);
nhan++;
LATA = nhan;
}
if(!PORTDbits.RD6)
{
while(!PORTDbits.RD6);
while(1)
{
adc = analogRead(5);
R = (9000.0*adc/1023.0+1000);
Timer_Init(4000);
if(Timer_flag)
{
if(ms>=R)
{
ms = 0;
if(LATA == nhan)
LATA = 0;
else
LATA = nhan;
}
Timer_flag = 0;
}
}
}
}
return 0;
}

You might also like