Code Final
Code Final
#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_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;
}
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"
#include "xc.h"
#define FCY 8000000UL
#include "libpic30.h"
#include "lcd.h"
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"
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;
}