---
# System prepended metadata

title: 介面lab03

---

# 介面lab03
## 工作日誌
* 8/10閱讀講義
* 8/11硬體配線~8/14 debug
* 8/14完成
## 程式碼
### 1. 開發ASA BUS 溫度感測介面卡
```c
#include "c4mlib.h"
#define F_CPU 11059200UL

int main(void)
{
	C4M_DEVICE_set();
	
	/*SPI串列通訊埠設定旗標群*/
	REGFPT(&SPSR,0x01,0,1);//選擇雙倍工作時脈
	REGFPT(&SPCR,0x03,0,0);//設定工作時脈除頻值4
	REGFPT(&SPCR,0x04,2,0);//同步時脈讀寫 前收後送
	REGFPT(&SPCR,0x08,3,0);//同步時脈前緣 前緣為上
	REGFPT(&SPCR,0x10,4,1);//主僕選擇 主
	REGFPT(&SPCR,0x20,5,0);//SPI資料傳輸順序 高位元先送
	REGFPT(&DDRB,0x0F,0,7);//設定 MOSI、SCK、SS 輸出，MISO 接腳為輸入
	REGFPT(&DDRF,0xE0,5,7);//設定 ASAID 接腳為輸出
	REGFPT(&DDRF,0x0F,0,1);//設定SPI SS8 PF0接腳為CS
	
	/*SPI串列通訊埠控制旗標群*/
	REGFPT(&SPCR,0x80,7,0);//SPI中斷禁能
	REGFPT(&SPCR,0x40,6,1);//SPI致能
	REGFPT(&PORTF,1,0,1);//CS初始值1
	
	float Temp=0;//存放最終浮點數溫度資料
	uint16_t temp=0;//存放讀回之定點數資料
	
	while(1)
	{
		_delay_ms(500);
		REGFPT(&PORTF,1,0,0);
		ASA_SPIM_rec(4, 8, 0, 2, &temp, 1);//讀值
		_delay_ms(500);
		REGFPT(&PORTF,1,0,1);
		//temp = temp >> 3;
		Temp = temp / 128;
		printf("Temp = %f \n", Temp);
		//printf("temp = %d \n", temp);
	}
	
}
```
### 2. 開發ASA BUS LED矩陣顯示器介面卡
```c
#include "c4mlib.h"
#define F_CPU 11059200UL

void Display(float Temp){
	
	
	uint8_t Values[4],prinum,zero=0;
	int Quotient,remainder;
	//uint8_t SevenSegTable[]={0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111,0b10000000,0b00111001,0b01110001};
	uint8_t SevenSegTable[]={0,1,2,3,4,5,6,7,8,9};
	float Value;
	
	Value=Temp;
	//printf("Value=%f\n",Value);
	
	if (Value==0.0)
	{
		//printf("Value=0\n");
		Values[0]=0b00111111;
		Values[1]=0b00111111;
		Values[2]=0b00111111;
		Values[3]=0;
	}
	else
	{
		Quotient=Value*100;
		remainder=0;
		prinum=4;
		for (int i=1;i<=prinum;i++)
		{
			remainder=Quotient-(Quotient/10)*10;
			Quotient=(Quotient-remainder)/10;
			Values[i-1]=SevenSegTable[remainder];
			if (remainder!=0)zero=i;
			//printf("remainder=%d\n",remainder);
		}
		for (int j=zero+1;j<=prinum;j++)
		{
			Values[j-1]=255;
		}
	}
	
	Values[2]=Values[2]|0b10000000;//輸出小數點
	
	for (int j=1;j<=4;j++)
	{
		//_delay_ms(600);
		//printf("j=%d",j);
		REGFPT(&PORTF,1,0,0);
		ASA_SPIM_trm(6, 9, j, 1, &Values[j-1], 10);//輸出資料
		//ASA_SPIM_ftm(6, 9, 0, j, 0, 0, &SevenSegTable[j], 10);
		_delay_ms(10);
		REGFPT(&PORTF,1,0,1);
		
	}
}

float RaedTemperature(){
	
	float Temp=0;//存放最終浮點數溫度資料
	uint16_t temp=0;//存放讀回之定點數資料
	
	_delay_ms(100);
	REGFPT(&PORTF,1,0,0);
	ASA_SPIM_rec(4, 8, 0, 2, &temp, 1);//讀值
	_delay_ms(100);
	REGFPT(&PORTF,1,0,1);
	//temp = temp >> 3;
	Temp = temp / 128;
	printf("Temp = %f \n", Temp);
	//printf("temp = %d \n", temp);
	
	return Temp;
}


int main(void)
{
	C4M_DEVICE_set();
	
	/*SPI串列通訊埠設定旗標群*/
	REGFPT(&SPSR,0x01,0,1);//選擇雙倍工作時脈
	REGFPT(&SPCR,0x03,0,0);//設定工作時脈除頻值4
	REGFPT(&SPCR,0x04,2,0);//同步時脈讀寫 前收後送
	REGFPT(&SPCR,0x08,3,0);//同步時脈前緣 前緣為上
	REGFPT(&SPCR,0x10,4,1);//主僕選擇 主
	REGFPT(&SPCR,0x20,5,0);//SPI資料傳輸順序 高位元先送
	REGFPT(&DDRB,0x0F,0,7);//設定 MOSI、SCK、SS 輸出，MISO 接腳為輸入
	REGFPT(&DDRF,0xE0,5,7);//設定 ASAID 接腳為輸出
	REGFPT(&DDRF,0x0F,0,1);//設定SPI SS8 PF0接腳為CS
	REGFPT(&DDRF,0x0F,0,1);//設定SPI SS9 PF1接腳為CS
	
	/*SPI串列通訊埠控制旗標群*/
	REGFPT(&SPCR,0x80,7,0);//SPI中斷禁能
	REGFPT(&SPCR,0x40,6,1);//SPI致能
	REGFPT(&PORTF,1,0,1);//CS初始值1
	REGFPT(&PORTF,2,0,2);//CS初始值1
	
	/*MAX7219 設定參數*/
	uint8_t decode=0x0F;
	uint8_t shutdown=1;
	uint8_t scanlimit=0x03;
	uint8_t intensity=0x0F;
	uint8_t test=0;
	
	REGFPT(&PORTF,1,0,0);
	ASA_SPIM_trm(6, 9, 9, 1, &decode, 10);//全不解碼
	//ASA_SPIM_ftm(6, 9, 9, 1 , 0, 0 , &decode, 10);
	_delay_ms(10);
	REGFPT(&PORTF,1,0,1);
	
	_delay_ms(10);
	
	REGFPT(&PORTF,1,0,0);
	ASA_SPIM_trm(6, 9, 10, 1, &intensity, 10);//15亮度
	//ASA_SPIM_ftm(6, 9, 10, 1 , 0, 0 , &intensity, 10);
	_delay_ms(10);
	REGFPT(&PORTF,1,0,1);
	
	_delay_ms(10);
	
	REGFPT(&PORTF,1,0,0);
	ASA_SPIM_trm(6, 9, 11, 1, &scanlimit, 10);//顯示位數3+1
	//ASA_SPIM_ftm(6, 9, 11, 1 , 0, 0 , &scanlimit, 10);
	_delay_ms(10);
	REGFPT(&PORTF,1,0,1);

	_delay_ms(10);
	
	REGFPT(&PORTF,1,0,0);
	ASA_SPIM_trm(6, 9, 12, 1, &shutdown, 10);//全開燈
	//ASA_SPIM_ftm(6, 9, 12, 1 , 0, 0 , &shutdown, 10);
	_delay_ms(10);
	REGFPT(&PORTF,1,0,1);
	
	_delay_ms(10);
	
	REGFPT(&PORTF,1,0,0);
	ASA_SPIM_trm(6, 9, 15, 1, &test, 10);//強制全亮
	//ASA_SPIM_ftm(6, 9, 15, 1 , 0, 0 , &test, 10);
	_delay_ms(10);
	REGFPT(&PORTF,1,0,1);
	
	
	float temp;
	
	while(1)
	{
		temp=RaedTemperature();
		Display(temp);
		_delay_ms(500);
	}
	
}
```

## 流程圖
### 1. 開發ASA BUS 溫度感測介面卡
![](https://i.imgur.com/kjgG7Ac.png)

### 2. 開發ASA BUS LED矩陣顯示器介面卡 
![](https://i.imgur.com/M8bVlH8.png)



## 實驗數據

### 1. 開發ASA BUS 溫度感測介面卡
* PORTF0，做為CS，其編號為8。![](https://i.imgur.com/DysouXv.jpg)
![](https://i.imgur.com/C6sKo1v.png)
### 2. 開發ASA BUS LED矩陣顯示器介面卡
* PORTF0，做為TMP121的CS，其編號為8。
* PORTF0，做MAX7219的CS，其編號為9。因為PORTF1無法成功拉LOW，所以改與TMP121共用CS。
![](https://i.imgur.com/eICDU3J.jpg)
![](https://i.imgur.com/c43jBlk.png)



## 驗收
### 驗收題目
* 七節管改為顯示華氏溫度，並顯示小數後兩位的值

### 驗收成果
![](https://i.imgur.com/x2UpfsA.png)
![](https://i.imgur.com/HocUKXQ.jpg)
![](https://i.imgur.com/M5dPZL1.jpg)

