Ðăng Nhập

View Full Version : Lập trình PIC16F887 bằng ngôn ngữ MikroC


orionduc
12-07-2011, 01:12 AM
Chào các bạn!
Mình vừa mới bắt đầu học về lập trình PIC bằng ngôn ngữ MikroC nhưng lại khó khăn là mình đã lang thang khắp các diễn đàn để tìm tài liệu hướng dẫn MikroC nhưng không thấy. Bạn nào có tài liệu nào về MikroC thì cho mình xin nha! Cảm ơn các bạn nhiều!
Email của mình: orionduc@gmail.com

nguyen binh
12-07-2011, 04:28 PM
học MikroC rất đơn giản chỉ cần vô phần help là có đủ cả. ở đây có các đoạn code mẩu cho pic16f887

thanhtb_89
12-07-2011, 04:50 PM
các anh ui
các anh xem hộ em chuơng trình đọc dữ liệu từ lm35 hiển thị ra led 7 thanh qua 2 ic 74595
các anh xem giúp em nhá

thanhtb_89
12-07-2011, 04:52 PM
đây là code của em


#include <16F886.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES PROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOIESO //No Internal External Switch Over mode enabled
#FUSES NOFCMEN //No Fail-safe clock monitor enabled
#FUSES NOLVP //No Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NODEBUG //No Debug mode for ICD
#FUSES WRT //Program memory write protected
#FUSES BORV40 //Brownout reset at 4v

#use delay(clock=4000000)


#byte PORTA = 0x05
#byte TRISA = 0x85

#byte PORTB = 0x06
#byte TRISB = 0x86

#byte PORTC = 0x07
#byte TRISC = 0x87

#byte ANSELL = 0x188
#byte ANSELH = 0x189
#byte ANSELL = 0x188

#byte SSPCON = 0x14
#byte SSPCON2 = 0x91
#byte SSPADD = 0x93
#byte SSPSTAT = 0x94

#byte SSPCON = 0x14
#byte SSPCON2 = 0x91
#byte SSPADD = 0x93
#byte SSPSTAT = 0x94

#byte PORTB = 0x06
#byte TRISB = 0x86
#bit rb5 = portb.5

//===================================
//define
//===================================


#define MASK_LED_0 0b00101000
#define MASK_LED_1 0b10101111
#define MASK_LED_2 0b10011000
#define MASK_LED_3 0b10001010
#define MASK_LED_4 0b00001111
#define MASK_LED_5 0b01001010
#define MASK_LED_6 0b01001000
#define MASK_LED_7 0b10101110
#define MASK_LED_8 0b00001000
#define MASK_LED_9 0b00001010

#define led_1 led[0] //du lieu hien tren 4 led 7 thanh
#define led_2 led[1]
#define led_3 led[2]
#define led_4 led[3]

#define hien_thi_led_1 0b00001000
#define hien_thi_led_2 0b00000010
#define hien_thi_led_3 0b00000100
#define hien_thi_led_4 0b00000001
const unsigned char hien_thi_led[] ={hien_thi_led_1,hien_thi_led_2,hien_thi_led_3,hie n_thi_led_4};
unsigned char led[8];


char value;


//===================================
//KHAI BAO CHUONG TRINH CON
//===================================
void HEX_BCD();
void HIENTHI();
void lat();


//===================================
//chuong trinh doc adc
//===================================
void main()
{
int8 value;
set_tris_A(1);

//===================================
//khoi tao che do cho ADC
//===================================

Setup_ADC(ADC_CLOCK_INTERNAL);//khoi tao ADC
setup_ADC_ports(sAN0|VSS_VDD);
Set_ADC_channel(0);
delay_us(10); //ket thuc khoi tao ADC

//===================================
//lay mau nhiet do cho lan dau tien
//===================================
while(true)
{
value=read_ADC();

//===================================
//chuong trinh hien thi
//===================================

HEX_BCD();
HIENTHI();
}
}

//================================================== ====================
//tach lay so hang tram, hang chuc, hang don vi de hien thi ra led
//================================================== ====================

void HEX_BCD()
{
int16 value;
led_1=value/1000;
value=value%1000;
led_2=value/100;
value=value%100;
led_3=value/10;
led_4=value%10;
}

//================================================== ====================
//chuong trinh latch
//================================================== ====================

void lat()
{
output_high(PIN_B3);
output_low(PIN_B3);

}

//================================================== ====================
//HIEN THI
//================================================== ====================
void HIENTHI()
{
unsigned char mask,i,n,k;
int1 data;
set_tris_B(0);
while(1)
{
for(n=4;n>=0;n--)
{
k=led[n];
for(i=8;i>=0;i--)
{
data = bit_test(k,n-1);
output_bit(pin_b5,data);
output_high(PIN_B4);
output_low(PIN_B4);

}
}


mask = hien_thi_led[n];
for(i=8;i>=0;i--)
{
data = bit_test(mask,i-1);
output_bit(pin_b5,data);
output_high(PIN_B4);
output_low(PIN_B4);
lat();
}

}
}