22H1120059 Lab5
22H1120059 Lab5
a)
b)char dulieu='\0';
#define led 9
void serialEvent() {
while (Serial.available()) {
dulieu=inChar;
void setup() {
Serial.begin(9600);
delay(100);
pinMode(led,OUTPUT);
void loop() {
switch(dulieu)
case '1':
digitalWrite(led,HIGH);
case '2':
digitalWrite(led,LOW);
default:
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
public Form1()
InitializeComponent();
UART.BaudRate = 9600;
UART.PortName = "COM1";
UART.Open();
UART.Write("1");
{
UART.Write("2");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
/// <summary>
/// </summary>
[STAThread]
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Bài 2:
#define _7447_A 6
#define _7447_B 5
#define _7447_C 4
#define _7447_D 3
#define max_buffer 17
char Buffer[max_buffer];
char Heso=0;
char dulieu='\0';
char so_dat;
boolean kiemtra=0;
void serialEvent() {
while (Serial.available()) {
if(inChar!='\n')
Buffer[Heso]= inChar-48;
Heso++;
if(Heso>=max_buffer)
Heso=0;
else
so_dat=Buffer[0];
for(Heso=0;Heso<max_buffer;Heso++)
Buffer[Heso]='\n';
Heso=0;
// so_dat=inChar-48;
}
void setup() {
Serial .begin(9600);
delay(100);
pinMode(_7447_A,OUTPUT);
pinMode(_7447_B,OUTPUT);
pinMode(_7447_C,OUTPUT);
pinMode(_7447_D,OUTPUT);
void loop() {
byte i=0;
nap_bcd(so_dat);
delay(100);
Serial.println(so_dat);
// for(i=0;i<=9;i++)
// {
// nap_bcd(i);
// delay(100);
// }
// delay(100);
// switch(dulieu)
// {
// case 'A':
// {
// for(i=0;i<=9;i++)
// {
// nap_bcd(i);
// delay(100);
// dulieu='\0';
// }
// dulieu='\0';
// break;
// }
// case 'B':
// {
// for(i=9;i>=0;i--)
// {
// nap_bcd(i);
// delay(100);
// dulieu='\0';
// }
// dulieu='\0';
// break;
// }
// default:
// {
// dulieu='\0';
// }
// }
switch(BCD)
case 0:
digitalWrite(_7447_A,LOW);
digitalWrite(_7447_B,LOW);
digitalWrite(_7447_C,LOW);
digitalWrite(_7447_D,LOW);
break;
case 1:
digitalWrite(_7447_A,HIGH);
digitalWrite(_7447_B,LOW);
digitalWrite(_7447_C,LOW);
digitalWrite(_7447_D,LOW);
break;
case 2:
{
digitalWrite(_7447_A,LOW);
digitalWrite(_7447_B,HIGH);
digitalWrite(_7447_C,LOW);
digitalWrite(_7447_D,LOW);
break;
case 3:
digitalWrite(_7447_A,HIGH);
digitalWrite(_7447_B,HIGH);
digitalWrite(_7447_C,LOW);
digitalWrite(_7447_D,LOW);
break;
case 4:
digitalWrite(_7447_A,LOW);
digitalWrite(_7447_B,LOW);
digitalWrite(_7447_C,HIGH);
digitalWrite(_7447_D,LOW);
break;
case 5:
{
digitalWrite(_7447_A,HIGH);
digitalWrite(_7447_B,LOW);
digitalWrite(_7447_C,HIGH);
digitalWrite(_7447_D,LOW);
break;
case 6:
digitalWrite(_7447_A,LOW);
digitalWrite(_7447_B,HIGH);
digitalWrite(_7447_C,HIGH);
digitalWrite(_7447_D,LOW);
break;
case 7:
digitalWrite(_7447_A,HIGH);
digitalWrite(_7447_B,HIGH);
digitalWrite(_7447_C,HIGH);
digitalWrite(_7447_D,LOW);
break;
case 8:
digitalWrite(_7447_A,LOW);
digitalWrite(_7447_B,LOW);
digitalWrite(_7447_C,LOW);
digitalWrite(_7447_D,HIGH);
break;
case 9:
digitalWrite(_7447_A,HIGH);
digitalWrite(_7447_B,LOW);
digitalWrite(_7447_C,LOW);
digitalWrite(_7447_D,HIGH);
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
int dem = 0;
uint tam;
#region quan_li_ham
cbxTencom.DataSource = SerialPort.GetPortNames();
if (cbxTencom.Items.Count > 1)
cbxTencom.SelectedIndex = 1;
#endregion
#region quam_li_form
public Form1()
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
khoi_tao();
switch (e.KeyCode)
case Keys.Escape:
Close();
break;
default:
break;
}
}
#endregion
try
UART.BaudRate = 9600;
UART.PortName=cbxTencom.Text;
UART.Open();
timer1.Enabled = true;
if (UART.IsOpen == true)
MessageBox.Show(ex.Message);
try
{
UART.Close();
timer1.Enabled = false;
if (UART.IsOpen == false)
MessageBox.Show(ex.Message);
if (UART.IsOpen == true)
UART.Write(tx_send.Text.Trim() + "\n");
dem = 0;
{
dem = 1;
dem = 2;
if (dem == 1)
tam = Convert.ToUInt32(tx_send.Text.Trim());
tam++;
tx_send.Text= tam.ToString();
UART.Write(tx_send.Text.Trim()+ "\n");
if (tam > 9)
tam = 1;
else if (dem == 2)
{
tam = Convert.ToUInt32(tx_send.Text.Trim());
tam--;
tx_send.Text = tam.ToString();
UART.Write(tx_send.Text.Trim() + "\n");
if (tam <= 0)
tam = 9;
else
dem = 0;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
/// <summary>
/// </summary>
[STAThread]
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Bài 3:
#include "HC595.h"
HC595 _HC595_LED;
HC595 _HC595_COT;
#define cambien1 A0
#define cambien2 A1
#define cambien3 A2
#define RESET A3
byte cacso[]={0,1,2,3,4,5};
char chuoi1[50];
char sothutu1=0,sothutu2=0,sothutu3=0;
void tathetled(void);
void setup() {
Serial.begin(9600);
delay(100);
_HC595_LED._KhoiDongHC595(7,8,9);
_HC595_COT._KhoiDongHC595(4,5,6);
pinMode(cambien1,INPUT);
pinMode(cambien2,INPUT);
pinMode(cambien3,INPUT);
pinMode(RESET,INPUT);
tathetled();
void loop() {
while(1)
if(digitalRead(cambien1)==0)
while(digitalRead(cambien1)==0)
Hienthiled(cacso);
sothutu1=sothutu1+1;
if(digitalRead(cambien2)==0)
while(digitalRead(cambien2)==0)
Hienthiled(cacso);
sothutu2=sothutu2+1;
if(digitalRead(cambien3)==0)
{
while(digitalRead(cambien3)==0)
Hienthiled(cacso);
sothutu3=sothutu3+1;
if(digitalRead(RESET)==0)
while(digitalRead(RESET)==0)
Hienthiled(cacso);
sothutu3=0;
sothutu2=0;
sothutu1=0;
sprintf(chuoi1,"%02d,%02d,%02d",sothutu1,sothutu2,sothutu3);
Serial.println(chuoi1);
_Tach2so(sothutu1,&cacso[0],&cacso[1]);
_Tach2so(sothutu2,&cacso[2],&cacso[3]);
_Tach2so(sothutu3,&cacso[4],&cacso[5]);
Scandelay(cacso,100);
}
}
*donvi=dem%10;
dem=dem/10;
*chuc=dem%10;
byte i;
char Mang[8];
const byte
MaLed[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF};//neu
la A chung
//byte
MaLed[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};//neu
la K chung
byte Madich=0x01;
for(i=0;i<=7;i++)
Mang[i]=MaLed[Cacso[i]];
for(i=0;i<=7;i++)
_HC595_COT._TruyenByte(Madich);
_HC595_LED._TruyenByte(Mang[i]);
delay(1);
_HC595_COT._TruyenByte(0x00);
Madich=Madich<<1;
t=t/6;
while(t>0)
t=t-1;
Hienthiled(CacSo);
delay(1);
void tathetled(void)
_HC595_COT._TruyenByte(0x00);
_HC595_LED._TruyenByte(0x00);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
#region quan_li_ham
cbcTencom.DataSource = SerialPort.GetPortNames();
if (cbcTencom.Items.Count > 1)
cbcTencom.SelectedIndex = 1;
#endregion
#region quan_li_form
public Form1()
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
khoitao();
switch (e.KeyCode)
{
case Keys.Escape:
Close();
break;
default:
break;
#endregion
try
UART.BaudRate = 9600;
UART.PortName = cbcTencom.Text;
UART.Open();
if (UART.IsOpen == true)
{
MessageBox.Show("ket noi thanh cong");
MessageBox.Show(ex.Message);
try
UART.Close();
if (UART.IsOpen == false)
{
MessageBox.Show(ex.Message);
temp = UART.ReadTo("\n");
if (tach.Length >= 3)
tx_may1.Text = tach[0].ToString();
tx_may2.Text = tach[1].ToString();
tx_may3.Text = tach[2].ToString();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
/// <summary>
/// </summary>
[STAThread]
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());