Vi Điều Khiển

Chào mừng các bạn đến với thế giới của Vi điều khiển!

--welcome to the world of microcontrollers^^ --

Điện Tử Cơ Bản

nơi khởi đầu

Lập Trình

linh hồn của phần cứng

Hiển thị các bài đăng có nhãn truyền thông nối tiếp RS232. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn truyền thông nối tiếp RS232. Hiển thị tất cả bài đăng

Thứ Năm, 12 tháng 4, 2012

Code mẫu PIC - Giao Tiếp I2C Với IC Thời gian thực DS1307

.
Tham khảo thêm: Hướng dẫn - IC thời gian thực DS1307

Chương trình thực hiện giao tiếp I2C giữa PIC 16F877A và IC DS1307 để cài đặt thời gian, đọc thời gian từ DS1307, hiển thị lên LCD, truyền qua RS232.
Với LCD, chương trình sẽ đọc dữ liệu DS1307 và cập nhật LCD liên tục, còn khi truyền lên máy tính sẽ dựa vào ngắt RB0 đưa vào từ xung 1Hz của DS1307, tức là mỗi 1s sẽ truyền dữ liệu 1 lần.
Trong chương trình có sử dụng các thư viện: lcd_lib_4bit.cds1307.c.

Sơ đồ nguyên lý mô phỏng trên Proteus:



Truyền lên hyper terminal:



Mã nguồn chương trình chính:



#include "16f877a.h"
#include "def_877a.h"


#device *=16 ADC=10
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use i2c(Master, sda = PIN_C4, scl=PIN_C3)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


#include "lcd_lib_4bit.c"
#include "ds1307.c"


#define Slave_add 0x68
#define Read 1
#define Write 0


void send(int8 a);


int8 sec,min,hrs,day,month,yr,dow;


//ngat o chan RB0: Truyen len cong RS232
#int_EXT
void EXT_isr(void) //moi 1s truyen len may tinh 1 lan
{
   ds1307_get_date(day,month,yr,dow); 
   ds1307_get_time(hrs,min,sec);
   send(hrs);
   putc(45);
   send(min);
   putc(45);
   send(sec);
   putc(10);
   return;
}
void main()
{
   enable_interrupts(INT_EXT);//cho phep ngat RB0
   ext_int_edge(0,H_TO_L);//dat suon ngat
   enable_interrupts(GLOBAL);//cho phep ngat toan cuc
   
   LCD_init(); //Khoi tao LCD.
   delay_ms(10);
   ds1307_init();// khoi tao DS1307, tao xung 1Hz o chan 7 DS1307.
   // Set date : 12-4-2012
   // Set time : thu 5 - 12 gio, 59 phút 10 giây
   ds1307_set_date_time(12,4,12,5,12,59,10);
   


   while(1) 
   { 
      ds1307_get_date(day,month,yr,dow); 
      ds1307_get_time(hrs,min,sec);
      
      //Truyen len LCD
      //o day chi hien gio, phut, giay. cac thong tin khac thuc hien tuong tu.
      LCD_PutCmd(0x80);
      LCD_PutChar(hrs/10+48);
      LCD_PutChar(hrs%10+48);
      LCD_PutChar(45);
      LCD_PutChar(min/10+48);
      LCD_PutChar(min%10+48);
      LCD_PutChar(45);
      LCD_PutChar(sec/10+48);
      LCD_PutChar(sec%10+48);
   }
}
void send(int8 a)
{
    if(a<10)
     {
         putc(a+48);         
     }
     if(a>9&&a<100)
     {
          unsigned char c=a/10;
          unsigned char d=a%10;
          putc(c+48);
          putc(d+48);       
     }
     if(a>99)
     {
          unsigned char t=a/100;
          unsigned char c=a/10-10*t;
          unsigned char d=a%10;
          putc(t+48);
          putc(c+48);
          putc(d+48);
     }
}


Thư viện lcd_lib_4bit.c:


#include <stddef.h>


#define LCD_RS          PIN_D2
//#define LCD_RW          PIN_A1
#define LCD_EN          PIN_D3


#define LCD_D4          PIN_D4
#define LCD_D5          PIN_D5
#define LCD_D6          PIN_D6
#define LCD_D7          PIN_D7


// misc display defines-
#define Line_1          0x80
#define Line_2          0xC0
#define Clear_Scr       0x01


// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi len LCD
#separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );// Xung kich hoat
#separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data
// D/n Cong
#use standard_io (C)
#use standard_io (D)


//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
    {
    LCD_SetData ( 0x00 );
    delay_ms(200);       /* wait enough time after Vdd rise >> 15ms */
    output_low ( LCD_RS );// che do gui lenh
    LCD_SetData ( 0x03 );   /* init with specific nibbles to start 4-bit mode */
    LCD_PulseEnable();
    LCD_PulseEnable();
    LCD_PulseEnable();
    LCD_SetData ( 0x02 );   /* set 4-bit interface */
    LCD_PulseEnable();      /* send dual nibbles hereafter, MSN first */
    LCD_PutCmd ( 0x2C );    /* function set (all lines, 5x7 characters) */
    LCD_PutCmd ( 0x0C );    /* display ON, cursor off, no blink */
    LCD_PutCmd ( 0x06 );    /* entry mode set, increment & scroll left */
    LCD_PutCmd ( 0x01 );    /* clear display */
    }


#separate void LCD_SetPosition ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
    LCD_SetData ( swap ( cX ) | 0x08 );
    LCD_PulseEnable();
    LCD_SetData ( swap ( cX ) );
    LCD_PulseEnable();
    }


#separate void LCD_PutChar ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
        output_high ( LCD_RS );
        LCD_PutCmd( cX );
        output_low ( LCD_RS );
    }


#separate void LCD_PutCmd ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
    LCD_SetData ( swap ( cX ) );     /* send high nibble */
    LCD_PulseEnable();
    LCD_SetData ( swap ( cX ) );     /* send low nibble */
    LCD_PulseEnable();
    }
#separate void LCD_PulseEnable ( void )
    {
    output_high ( LCD_EN );
    delay_us ( 3 );         // was 10
    output_low ( LCD_EN );
    delay_ms ( 3 );         // was 5
    }


#separate void LCD_SetData ( unsigned int cX )
    {
    output_bit ( LCD_D4, cX & 0x01 );
    output_bit ( LCD_D5, cX & 0x02 );
    output_bit ( LCD_D6, cX & 0x04 );
    output_bit ( LCD_D7, cX & 0x08 );
    }

Thư viện ds1307.c:


BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);


void ds1307_init(void) 

   BYTE initsec = 0;
   BYTE initmin=0;
   BYTE inithr=0;                  
   BYTE initdow=0;
   BYTE initday=0;                   
   BYTE initmth=0;
   BYTE inityear=0;
   i2c_start(); 
   i2c_write(0xD0);      // WR to RTC 
   i2c_write(0x00);      // REG 0 
   i2c_start(); 
   i2c_write(0xD1);      // RD from RTC 
   initsec  = bcd2bin(i2c_read() & 0x7f); 
   initmin  = bcd2bin(i2c_read() & 0x7f); 
   inithr   = bcd2bin(i2c_read() & 0x3f); 
   initdow  = bcd2bin(i2c_read() & 0x7f);   // REG 3 
   initday  = bcd2bin(i2c_read() & 0x3f);   // REG 4 
   initmth  = bcd2bin(i2c_read() & 0x1f);   // REG 5 
   inityear = bcd2bin(i2c_read(0));         // REG 6 
   i2c_stop(); 
   delay_us(3);


   i2c_start(); 
   i2c_write(0xD0);      // WR to RTC 
   i2c_write(0x00);      // REG 0 
   i2c_write(bin2bcd(initsec));      // Start oscillator with current "seconds value 
   i2c_write(bin2bcd(initmin));      // REG 1 
   i2c_write(bin2bcd(inithr));       // REG 2 
   i2c_write(bin2bcd(initdow));      // REG 3 
   i2c_write(bin2bcd(initday));      // REG 4 
   i2c_write(bin2bcd(initmth));      // REG 5 
   i2c_write(bin2bcd(inityear));     // REG 6 
   i2c_start(); 
   i2c_write(0xD0);      // WR to RTC 
   i2c_write(0x07);      // Control Register 
   i2c_write(0x90);      //  squarewave output pin 1Hz
   i2c_stop(); 





void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;


  i2c_start();
  i2c_write(0xD0);              // I2C write address
  i2c_write(0x00);              // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));       // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));     // REG 6
  i2c_write(0x90);              // REG 7 - 1Hz squarewave output pin
  i2c_stop();
}


void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);              // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));         // REG 6
  i2c_stop();
}


void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);                     // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();


}


BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;


  temp = binary_value;
  retval = 0;


  while(1)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  }


  return(retval);
}




// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;


  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;


  // Now return: (Tens * 8) + (Tens * 2) + Ones


  return(temp + (temp >> 2) + (bcd_value & 0x0f));





//tjtanja

Chủ Nhật, 26 tháng 2, 2012

Hướng Dẫn CCS Cho PIC - Bài 5: Truyền Thông Nối Tiếp RS232 - Xử Lý Chuỗi Trong CCS


Bài trước: 
+Bài 4: Chuyển Đổi ADC - Các Hàm Vào/Ra trong CCS
Bài sau: +Bài 6: Giao Tiếp SPI


Bài 5: Truyền Thông Nối Tiếp RS232 - Xử Lý Chuỗi Trong CCS




+ Phần này sẽ giúp bạn viết chương trình có sử dụng giao tiếp với máy tính (PC) . Điều này rất cần thiết khi bạn muốn VĐK khi hoạt động có thể truyền dữ liệu cho PC xử lý , hoặc nhận giá trị từ PC để xử lý và điều khiển ( dùng PC điều khiển động cơ , nhiệt độ , hay biến PC thành dụng cụ đo các đại lượng điện, Oscilocope , . . .) .

Thứ Sáu, 24 tháng 2, 2012

Code mẫu cho PIC - Truyền thông nối tiếp RS232

Việc giao tiếp giữa Vi điều khiển và máy tính là bài lập trình khá quan trọng khi ta làm việc với các dòng Vi điều khiển khác nhau. Với Vi điều khiển PIC cũng vậy, trong mỗi IC PIC đều có tích hợp một khối giao tiếp máy tính USART. Ta sử dụng khối giao tiếp này để truyền dữ liệu lên máy tính và xử lý dữ liệu đó tùy vào mục đích của người lập trình. Để nhận dữ liệu do Vi điều khiển truyền lên máy tính ta có thể sử dụng các phần mềm giao tiếp COM có sẵn hay viết một chương trình mới, sử dụng các ngôn ngữ lập trình như C++, VB hay Delphi… Trong chương trình ví dụ dưới đây tôi sử dụng công cụ sẵn có của CCS là Serial Port Monitor để truyền và nhận dữ liệu từ PIC.

Sơ đồ mạch điện ORCAD. Mạch sử dụng IC MAX232 để kết nối đến cổng COM của
máy tính. Mạch đơn giản chỉ nhằm mục đích giới thiệu khối giao tiếp máy tính của PIC và
cách lập trình cho nó trong CCS.


Trong chương trình ta có sử dụng hàm xử lý ngắt nối tiếp để xử lý ký tự nhân được từ máy
tính. Khi có ngắt xảy ra, ta gọi hàm getc() sẽ trả về ký tự vừa nhận được. Trên màn hình LCD
sẽ hiển thị ký tự mà ta gõ từ bàn phím máy tính.


 Mạch giao tiếp máy tính, hiển thị LCD

Mã nguồn chương trình: 

#include <16f877a.h> 
#include <def_877a.h> 
#use delay(clock=20000000) 
#FUSES  NOWDT,  HS,  NOPUT,  NOPROTECT,  NODEBUG,  NOBROWNOUT, 
NOLVP, NOCPD, NOWRT 
// Khai báo sử dụng giao tiếp nối tiếp RS232 
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9) 

#include <lcd_lib_4bit.c> 

int8 count=0; 
char string_in[16]; 

#INT_RDA  // Hàm xử lý ngắt nối tiếp 
Receive_isr() { 
char c; 
int8 i; 
count++; 
c = getc(); 
putc(c); 
if (c==ʹcʹ | c==ʹCʹ) 
   { 
      LCD_putcmd(0x01);    //Clear Screen 
      c=ʹcʹ; 
      count=0; 
   } 
if ((count<=16) && (c!=ʹcʹ))  LCD_putchar(c); 
if (count > 16) 
   { 
      count=0; 
      LCD_putcmd(0xC0); 
   } 
void main() 
{


enable_interrupts(int_rda);
enable_interrupts(GLOBAL);

lcd_init(); // Khởi tạo cho LCD
lcd_putcmd(0x01);
lcd_putcmd(line_1);
printf(ʺEnter a String.ʺ);
printf(ʺOr anything you want!ʺ);
while (1) {}
}


Mô tả chương trình: Trên đây là chương trình giao tiếp với máy tính, ta thấy trong CCS để sử dụng giao tiếp nối tiếp ta chỉ cần khai báo #use rs232(). Các hàm giao tiếp với máy tính mà CCS hỗ trợ là:
‐  putc(char ky_tu) : Gửi một ký tự ASCII lên máy tính
‐  getc() : Hàm trả về một ký tự nhận được từ máy tính
‐  printf(string): hàm gửi một chuỗi ký tự lên máy tính