PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Tìm Kiếm Bài Trong Ngày Ðánh Dấu Ðã Ðọc Vi điều khiển

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 27-05-2014, 01:10 AM   #1
daigiax92
Nhập môn đệ tử
 
Tham gia ngày: Sep 2012
Bài gửi: 1
:
Gửi chuỗi kí tự từ máy tính về pic để hiển thị LED-MATRIX với

Em đang làm đồ án về gửi 1 chuỗi kí tự từ máy tính thông qua chương trình giao diện c# về pic để hiện thị trên Led-Matrix
đây là code chương trình của em:
/////////////////////////////////////////////////////////////////////////////////////////////////
#include <16f877a.h>
#device 16f877a*=16 ADC=10
#include <stdlib.h>
#include <string.h>
#fuses nowdt,noprotect,nolvp,hs,put
#use delay(clock=20000000)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use rs232(baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7)
#byte porta=0x05
#byte portb=0x06
#byte portc=0x07
#byte portd=0x08
#bit b0=portb.0
#bit b1=portb.1
#bit b2=portb.2
#bit b3=portb.3
#bit b4=portb.4
#bit b5=portb.5

int dich_chu=0,toc_do_chay=0;
void lay_font(char * textptr);
unsigned char bi,temp,chieu_dai=0;
unsigned char hien_thi[96];
const BYTE TEXT[51][5] ={0x00, 0x00, 0x00, 0x00, 0x00, // SPACE
0x00, 0x00, 0x5F, 0x00, 0x00, // !
0x00, 0x03, 0x00, 0x03, 0x00, // "
0x14, 0x3E, 0x14, 0x3E, 0x14, // #
0x24, 0x2A, 0x7F, 0x2A, 0x12, // $
0x43, 0x33, 0x08, 0x66, 0x61, // %
0x36, 0x49, 0x55, 0x22, 0x50, // &
0x00, 0x05, 0x03, 0x00, 0x00, // '
0x00, 0x1C, 0x22, 0x41, 0x00, // (
0x00, 0x41, 0x22, 0x1C, 0x00, // )
0x14, 0x08, 0x3E, 0x08, 0x14, // *
0x08, 0x08, 0x3E, 0x08, 0x08, // +
0x00, 0x50, 0x30, 0x00, 0x00, // ,
0x08, 0x08, 0x08, 0x08, 0x08, // -
0x00, 0x60, 0x60, 0x00, 0x00, // .
0x20, 0x10, 0x08, 0x04, 0x02, // /
0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
0x04, 0x02, 0x7F, 0x00, 0x00, // 1
0x42, 0x61, 0x51, 0x49, 0x46, // 2
0x22, 0x41, 0x49, 0x49, 0x36, // 3
0x18, 0x14, 0x12, 0x7F, 0x10, // 4
0x27, 0x45, 0x45, 0x45, 0x39, // 5
0x3E, 0x49, 0x49, 0x49, 0x32, // 6
0x01, 0x01, 0x71, 0x09, 0x07, // 7
0x36, 0x49, 0x49, 0x49, 0x36, // 8
0x26, 0x49, 0x49, 0x49, 0x3E, // 9
0x00, 0x36, 0x36, 0x00, 0x00, // :
0x00, 0x56, 0x36, 0x00, 0x00, // ;
0x08, 0x14, 0x22, 0x41, 0x00, // <
0x14, 0x14, 0x14, 0x14, 0x14, // =
0x00, 0x41, 0x22, 0x14, 0x08, // >
0x02, 0x01, 0x51, 0x09, 0x06, // ?
0x3E, 0x41, 0x59, 0x55, 0x5E, // @
0x7E, 0x09, 0x09, 0x09, 0x7E, // A
0x7F, 0x49, 0x49, 0x49, 0x36, // B
0x3E, 0x41, 0x41, 0x41, 0x22, // C
0x7F, 0x41, 0x41, 0x41, 0x3E, // D
0x7F, 0x49, 0x49, 0x49, 0x41, // E
0x7F, 0x09, 0x09, 0x09, 0x01, // F
0x3E, 0x41, 0x41, 0x49, 0x3A, // G
0x7F, 0x08, 0x08, 0x08, 0x7F, // H
0x00, 0x41, 0x7F, 0x41, 0x00, // I
0x30, 0x40, 0x40, 0x40, 0x3F, // J
0x7F, 0x08, 0x14, 0x22, 0x41, // K
0x7F, 0x40, 0x40, 0x40, 0x40, // L
0x7F, 0x02, 0x0C, 0x02, 0x7F, // M
0x7F, 0x02, 0x04, 0x08, 0x7F, // N
0x3E, 0x41, 0x41, 0x41, 0x3E, // O
0x7F, 0x09, 0x09, 0x09, 0x06, // P
0x1E, 0x21, 0x21, 0x21, 0x5E, // Q
0x7F, 0x09, 0x09, 0x09, 0x76};// R

const BYTE TEXT2[44][5]={0x26, 0x49, 0x49, 0x49, 0x32, // S
0x01, 0x01, 0x7F, 0x01, 0x01, // T
0x3F, 0x40, 0x40, 0x40, 0x3F, // U
0x1F, 0x20, 0x40, 0x20, 0x1F, // V
0x7F, 0x20, 0x10, 0x20, 0x7F, // W
0x41, 0x22, 0x1C, 0x22, 0x41, // X
0x07, 0x08, 0x70, 0x08, 0x07, // Y
0x61, 0x51, 0x49, 0x45, 0x43, // Z
0x00, 0x7F, 0x41, 0x00, 0x00, // [
0x02, 0x04, 0x08, 0x10, 0x20, // \
0x00, 0x00, 0x41, 0x7F, 0x00, // ]
0x04, 0x02, 0x01, 0x02, 0x04, // ^
0x40, 0x40, 0x40, 0x40, 0x40, // _
0x00, 0x01, 0x02, 0x04, 0x00, // `
0x20, 0x54, 0x54, 0x54, 0x78, // a
0x7F, 0x44, 0x44, 0x44, 0x38, // b
0x38, 0x44, 0x44, 0x44, 0x44, // c
0x38, 0x44, 0x44, 0x44, 0x7F, // d
0x38, 0x54, 0x54, 0x54, 0x18, // e
0x04, 0x04, 0x7E, 0x05, 0x05, // f
0x08, 0x54, 0x54, 0x54, 0x3C, // g
0x7F, 0x08, 0x04, 0x04, 0x78, // h
0x00, 0x44, 0x7D, 0x40, 0x00, // i
0x20, 0x40, 0x44, 0x3D, 0x00, // j
0x7F, 0x10, 0x28, 0x44, 0x00, // k
0x00, 0x41, 0x7F, 0x40, 0x00, // l
0x7C, 0x04, 0x78, 0x04, 0x78, // m
0x7C, 0x08, 0x04, 0x04, 0x78, // n
0x38, 0x44, 0x44, 0x44, 0x38, // o
0x7C, 0x14, 0x14, 0x14, 0x08, // p
0x08, 0x14, 0x14, 0x14, 0x7C, // q
0x00, 0x7C, 0x08, 0x04, 0x04, // r
0x48, 0x54, 0x54, 0x54, 0x20, // s
0x04, 0x04, 0x3F, 0x44, 0x44, // t
0x3C, 0x40, 0x40, 0x20, 0x7C, // u
0x1C, 0x20, 0x40, 0x20, 0x1C, // v
0x3C, 0x40, 0x30, 0x40, 0x3C, // w
0x44, 0x28, 0x10, 0x28, 0x44, // x
0x0C, 0x50, 0x50, 0x50, 0x3C, // y
0x44, 0x64, 0x54, 0x4C, 0x44, // z
0x00, 0x08, 0x36, 0x41, 0x41, // {
0x00, 0x00, 0x7F, 0x00, 0x00, // |
0x41, 0x41, 0x36, 0x08, 0x00, // }
0x02, 0x01, 0x02, 0x04, 0x02};// ~

int8 i,m;
int8 n;
void dich(void);
void xuat(unsigned char bi);
void xoa_hien_thi();
#INT_RDA
void ngat()
{
char str[20];
gets(str);
set_tris_b(0b00000000);
xoa_hien_thi();//xóa vùng nho hien thi truoc
lay_font(str); // Write character set to matrix
for(dich_chu=0;dich_chu<=63;dich_chu++)
{
for(toc_do_chay=0;toc_do_chay<=2;toc_do_chay++)
{
b1=1;
b0=1;

b2=1;
b0=0;
b2=0;

for(m=0;m<=32;m++)
{
n=hien_thi[m+dich_chu];

n=~n;
xuat(n);
delay_us(1000);

dich();
}
}
}
printf("%s\r",str);

}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(global);

while(1);

}


void dich()
{
b1=0;
b0=1;
b0=0;
b2=1;
b2=0;
}
void xuat(unsigned char temp)
{
#bit flag_bit = temp.7
b4=0;
for(i=0;i<=7;i++)
{
if(flag_bit==1)
{b4=1;}
else
{b4=0;}
b3=1;
b3=0;
temp<<=1;
}
b5=1;
b5=0;
}
void lay_font(char * textptr)
{
int i, j;//
BYTE pixelData[5]; // Stores character data

for(i=0; textptr[i] != '\0'; ++i ) // Loop through the passed string
{
if(textptr[i] == '#')//textptr khai bao vi tri con tro
break;
if(textptr[i] < 'S') //ky tu nam o mang text
memcpy(pixelData, TEXT[textptr[i]-' '], 5);//copy 5 byte gia tri text[textptr[i]-'' vao pixeldata
else if(textptr[i] <= '~') // ky tu nam o mang text2
memcpy(pixelData, TEXT2[textptr[i]-'S'], 5);//copy 5 byte gia tri text2[textptr[i]-'s' vao pixeldata
else
memcpy(pixelData, TEXT[0], 5); // khoang trang
for(j=0;j<=4;j++)
{
hien_thi[chieu_dai]=(pixelData[j]);//hien thi tung ky tu
chieu_dai++;

}

}
}
void xoa_hien_thi(void)
{
chieu_dai=0;
for(m=0;m<=96;m++)
{
hien_thi[m]=0x00;
}
}
//////////////////////////////////////////////////////////////////////

Các bác xem đoạn lệnh này chắc em viết sai :3
//////////////////////////////////////////
#INT_RDA
void ngat()
{
char str[20];
gets(str);
set_tris_b(0b00000000);
xoa_hien_thi();//xóa vùng nho hien thi truoc
lay_font(str); // Write character set to matrix
for(dich_chu=0;dich_chu<=63;dich_chu++)
{
for(toc_do_chay=0;toc_do_chay<=2;toc_do_chay++)
{
b1=1;
b0=1;

b2=1;
b0=0;
b2=0;

for(m=0;m<=32;m++)
{
n=hien_thi[m+dich_chu];

n=~n;
xuat(n);
delay_us(1000);

dich();
}
}
}
printf("%s\r",str);

}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(global);

while(1);

}
////////////////////////////////////////////

Còn đây là code C#
////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Ports;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Doan
{
public partial class Form1 : Form
{
private SerialPort serPort;
public Form1()
{
InitializeComponent();
serPort = new SerialPort();
serPort.PortName = "COM8";
serPort.DataBits = 8;
serPort.Parity = Parity.None;
serPort.StopBits = StopBits.One;
}



private void Form1_Load(object sender, EventArgs e)
{
try
{
serPort.Open();
}
catch (Exception ex)
{
Debug.WriteLine("Error:" + ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
if (serPort.IsOpen)
{
string str = txtchu.Text;
serPort.Write(str);
}
}
}
}
///////////////////////////////////////

Dưới đây là hình ảnh để các bác dễ nhìn
Cám ơn các bác rất nhiều
Hình Kèm Theo
File Type: jpg 1.jpg (289.6 KB, 7 lần tải)
File Type: jpg 2.jpg (285.0 KB, 3 lần tải)
File Type: jpg 3.jpg (377.6 KB, 3 lần tải)
File Type: jpg 4.jpg (156.5 KB, 3 lần tải)
daigiax92 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 07:14 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam