将SPI设备由DS1302更改为开发板上的FLASH(W25QXX模块,注意该模块可以使用硬件SPI接口),主要工作为上位机发送数据给单片机,单片机将数据写入flash中;单片机在写入数据后,将这些数据读出,并在数码管上显示至少4个数据(注意尽量保证发送的是0-9,A-F,否则有可能显示失败),同时将所有数据发送到上位机串口显示,上位机上增加chart控件的数据显示,给出C#的上位机,和下位机的完整代码
时间: 2023-11-26 07:06:22 浏览: 37
下位机代码如下:
```c
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define CS_PIN 10 // W25QXX模块的片选引脚
void setup() {
Serial.begin(9600);
SPI.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // 初始化OLED屏幕
display.clearDisplay();
display.display();
}
void loop() {
if (Serial.available()) {
char data = Serial.read();
if (data == 'w') { // 如果收到w,则进行写操作
byte buffer[4];
for (int i = 0; i < 4; i++) {
while (!Serial.available()); // 等待数据传输完成
buffer[i] = Serial.read();
}
digitalWrite(CS_PIN, LOW); // 选中W25QXX模块
SPI.transfer(0x02); // 发送写指令
SPI.transfer(0x00); // 发送地址的高8位(因为FLASH容量不超过16MB,所以只需要发送3个字节的地址)
SPI.transfer(0x00); // 发送地址的中间8位
SPI.transfer(0x00); // 发送地址的低8位
for (int i = 0; i < 4; i++) {
SPI.transfer(buffer[i]); // 发送数据
}
digitalWrite(CS_PIN, HIGH); // 取消选中W25QXX模块
}
else if (data == 'r') { // 如果收到r,则进行读操作
byte buffer[4];
digitalWrite(CS_PIN, LOW); // 选中W25QXX模块
SPI.transfer(0x03); // 发送读指令
SPI.transfer(0x00); // 发送地址的高8位
SPI.transfer(0x00); // 发送地址的中间8位
SPI.transfer(0x00); // 发送地址的低8位
for (int i = 0; i < 4; i++) {
buffer[i] = SPI.transfer(0x00); // 读取数据
}
digitalWrite(CS_PIN, HIGH); // 取消选中W25QXX模块
for (int i = 0; i < 4; i++) {
Serial.write(buffer[i]); // 将读取到的数据发送给上位机
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Data:");
for (int i = 0; i < 4; i++) {
display.setCursor(0, 10 * (i + 1));
display.print(buffer[i], HEX); // 将读取到的数据显示在OLED屏幕上
}
display.display();
}
}
}
```
上位机代码如下:
```csharp
using System;
using System.IO.Ports;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace FlashReadWrite
{
public partial class Form1 : Form
{
private SerialPort port;
public Form1()
{
InitializeComponent();
port = new SerialPort("COM3", 9600); // 指定串口号和波特率
port.Open();
}
private void btnWrite_Click(object sender, EventArgs e)
{
if (tbxData.TextLength == 4)
{
port.Write("w"); // 发送写指令
for (int i = 0; i < 4; i++)
{
port.Write(tbxData.Text[i].ToString()); // 发送数据
}
}
else
{
MessageBox.Show("请输入4个字符!");
}
}
private void btnRead_Click(object sender, EventArgs e)
{
port.Write("r"); // 发送读指令
for (int i = 0; i < 4; i++)
{
tbxData.Text += ((char)port.ReadByte()).ToString(); // 读取数据并显示在文本框中
}
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (port.BytesToRead >= 4)
{
byte[] buffer = new byte[4];
port.Read(buffer, 0, 4); // 读取下位机发送的数据
this.Invoke(new Action(() =>
{
chart.Series[0].Points.Clear();
for (int i = 0; i < 4; i++)
{
chart.Series[0].Points.Add(buffer[i]); // 将数据显示在图表中
}
}));
}
}
}
}
```
注意:上位机代码需要使用Chart控件,需要在Visual Studio的工具箱中添加Chart控件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)