Ø
Phần
Design:
Ø
Phần
kết quả:
Ø
Code
CCS:
#include
<lcd4bit.c>
#include
<stdio.h>
#include
<stdlib.h>
#FUSES
HS,NOWDT,NOPUT,MCLR,NOPROTECT,NOCPD,NOBROWNOUT,IESO,FCMEN,NOLVP
#FUSES
NODEBUG
#FUSES
NOWRT
#FUSES
BORV40
#use
Fast_IO(B)
#use
delay( clock =20000000)
#use
rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define
ccp1 PIN_C6
#define
ccp2 PIN_C7
#define
en PIN_C4
UNSIGNED
int8 ctimer=0;
float
tocdo=0,tocdodat;
float
xung=0,xungmoi=0,xungcu=0;
float
dem=0, w=0;
void
ngatkenhA();
void
NGAT_TIMER1();
float
xung21(FLOAT);
#INT_TIMER1
void NGAT_TIMER1()
{
IF(dem>=4)
{
disable_interrupts(INT_EXT);
setup_timer_1(T1_DISABLED);
tocdo=3.45*xungmoi;
xungmoi=0;
set_timer1(15535);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts(INT_EXT);
dem=0;
}
dem=dem+1;
w=tocdo;
}
#INT_EXT
void ngatkenhA(void)
{
if(!input(PIN_B0))
{
xungmoi=xungmoi+1;
}
}
void main ()
{
char string[32];
gets(string);
tocdodat=120;
lcd_init();
set_tris_b(0x01);
set_tris_c(0x80);
set_tris_d(0x00);
output_d(0x00);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(15535);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
ext_INT_edge(H_TO_L);
enable_interrupts(GLOBAL);
while(true)
{
tocdodat=atof(string);
lcd_putcmd(0x80);
printf(lcd_putchar,"tocdo=%f",w);
printf("%f\n\r",tocdo);
delay_ms(500);
if(tocdo<tocdodat)
{
output_high(en);
output_low(ccp1);
output_high(ccp2);
lcd_putcmd(0xc0);
printf(lcd_putchar,"che do:
on-thuan");
}
if(tocdo>tocdodat)
{
output_low(en);
lcd_putcmd(0xc0);
printf(lcd_putchar,"che
do:off");
}
}
}
FILE LCD4bit.c :
#include
<stddef.h>
#define
LCD_RS PIN_E1
//#define
LCD_RW PIN_E1
#define
LCD_EN PIN_E0
#define
LCD_D4 PIN_B5
#define
LCD_D5 PIN_B4
#define
LCD_D6 PIN_B3
#define
LCD_D7 PIN_A2
//
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 ( B )
#use
standard_io (A)
//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 ( 0b00001100); /* 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 );
}
Ø
Code
giao diện basic 2010:
Imports System.IO.Ports
Imports System
Imports System.ComponentModel
Public Class Form1
Dim port As Array
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
port = IO.Ports.SerialPort.GetPortNames
chonCOM.Items.AddRange(port)
Timer1.Stop()
End Sub
Private Sub
Button_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button_connect.Click
With
SerialPort1
.PortName = chonCOM.Text
.BaudRate = 9600
.Parity = Parity.None
.StopBits = StopBits.One
.DataBits = 8
.ReceivedBytesThreshold = 1
End With
Try
Timer1.Start()
SerialPort1.Open()
SerialPort1.Write(TextBox_tocdodat.Text & vbCr)
Catch ex As Exception
MessageBox.Show("Đã kết nối", "Thông
báo", MessageBoxButtons.OK)
End Try
End Sub
Private Sub Button_exitprogram_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button_exitprogram.Click
If
MsgBox("Do you want to exit?", MsgBoxStyle.YesNo + MsgBoxStyle.Question,
"EXIT") = MsgBoxResult.Yes
Then
Close()
End If
End Sub
Private Sub ReceiveData(ByVal
sender As System.Object,
ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs)
Handles SerialPort1.DataReceived
CheckForIllegalCrossThreadCalls = False
Dim
data = SerialPort1.ReadLine
Me.Chart1.Series("TOC DO").Points.AddY(data)
End Sub
Private Sub Timer1_Tick(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Timer1.Tick
Dim dat
As Double
dat = Val(TextBox_tocdodat.Text)
Chart1.Series("TOC
DO DAT").Points.AddY(dat)
End Sub
Private Sub Button_disconnect_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button_disconnect.Click
Close()
End Sub
End Class