Đăng kí nhận bài viết qua Mail

Usart



 Đo nhiệt độ hiển thị kết quả trên màn hình máy tính
Download Hyper terminal for Win7 tại đây
Code
Soure file Usart.c
#include
#include
#include "i2c.h"

__CONFIG(HS & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS);        //1st config. word
__CONFIG(BORV21);        //2nd config. word

#define _XTAL_FREQ    4000000

//Global variables
    unsigned char cmd_byte;
    unsigned char temp;

//Interrupt Service Routine
void interrupt ISR(void)
{
}   

//Required main function
void main(void)
{
    char i,c;
    char buf[10];
   
    BRG16 = 0;
//    BAUDCTL = BAUDCTL & 0xF7;
    TXSTA = 0x24;    //Che do high-speed, cho phep phat du lieu
    RCSTA = 0x90;    //Nhan du lieu lien tuc, dung cac chan TX/RX
    //baud rate = Fosc / (16*(n+1))
    //n = Fosc/((baud rate)*16) - 1
    SPBRG = 25;    //Gia tri ung voi baud rate = 9600, xtal 4 MHz
    //SPBRGH khong can dung den
        TRISC3 = 1;            //configure PORTC<3> as input
    TRISC4 = 1;            //configure PORTC<4> as input
    TRISB = 0;            //make PORTB an output

//Set the slew rate and baud rate for 50 kHz operation
    SMP = 1;            //set for Standard speed slew rate
    SSPADD = 19;        //set the required baud rate (warning!)

//Configure as I2C master with Fosc/4 clock source
    SSPM3 = 1;            //set to I2C master mode with Fosc/4 clock source
    SSPEN = 1;            //Enable SDA and SCL pins to operate in I2C mode
    SSPCON2 = 0;        //ensure SSPCON2 is cleared

//Put '00000000' into variable cmd_byte.
//This is the read command that will be sent to the TC74
    cmd_byte = 0x00;
   
    while (1) {
    //    while(RCTF==0)
    //    C=RCREG;
    //    TXREG=c;
    //    while (TRMT==0)
            SEN = 1;            //Initiate a START condition
        while (SEN);        //
        SSPBUF = 0x90;        //send address of TC74 w/0 in the LSB (write action)
        while (RW);            //wait for write to complete
        while (ACKSTAT);    //Wait for ACK from TC74
       
        while (TRMT == 0);
        SSPBUF = cmd_byte;    //send READ command
        _delay(8);
        while (RW);
//------------------
        while (ACKSTAT);    //Get ACK from TC74
   
        while (TRMT == 0);
       
        RSEN = 1;            //send repeated start
        while (RSEN);
       
        SSPBUF = 0x91;        //send TC74 address with READ bit set
        _delay(8);
        while (RW);
//------------------
        while (ACKSTAT);
       
        while (TRMT == 0);    //Get ACK from TC74
        RCEN = 1;            //enable receive mode
        while (RCEN);        //wait until temperature data received
        temp = SSPBUF;
//------------------
        ACKDT = 1;            //NACK
        ACKEN = 1;            //send NACK condition
        while (ACKEN);
        PEN = 1;            //send STOP condition
        while (PEN);
//------------------
    //    PORTB = temp;
    //    while (GODONE);
    //    temp = ADRESH;
        //Gia su da co ket qua A/D
        //Xoa bo dem ky tu
       
        for (i = 0; i < 10; i++) buf[i] = 0;
        //Chuyen doi tu unsigned char sang ky tu ASCII
        utoa(buf, temp, 10);
       
        i = 0;
        while (buf[i] != 0) {
            TXREG = buf[i];
            i++;
            while (TRMT == 0);
        }
        TXREG = '\n';
        while (TRMT == 0);
        TXREG = '\r';
        for (i = 0; i < 10; i++) __delay_ms(100);
    }
}
Header file i2c.h

#ifndef    _I2C_H_
#define    _I2C_H_

void i2c_start();
void i2c_stop();
void i2c_init();
unsigned char i2c_read(unsigned char ack);
unsigned char i2c_write(unsigned char d);

#endif
Post a Comment
Related Posts Plugin for WordPress, Blogger...

Cùng tôi học Điện Tử ..(http://ilovec.tk)..