www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀] /* Title: Interrupt driven serial example(RS-485).* Author: Daidai Hu* Date: 06/2003* Purpose: Template for RS-485 slave communICation program.* Needed*SOFtware: AVR-GCC3.3 to compile* Ne

/* Title: Interrupt driven serial example(RS-485).
* Author: Daidai Hu
* Date: 06/2003
* Purpose: Template for RS-485 slave communICation program.

* Needed
*SOFtware: AVR-GCC3.3 to compile
* Needed
* Hardware: ATMega103 board with serial 0 connected to RS-485 bus. */

#include
#include
#include
#include
#include
#include
#include PACe.h>
#include

#define F_CPU 6000000 /* 6MHz. */
#define MAX_RCV_LEN 128 /* Maximal receive message length. */
#define MAX_SND_LEN 128 /* Maximal send message length. */

#defineUART_BAUD_SELECT(bps) (F_CPU/((bps)*16L)-1)

#define TICK 2 /* System tick(ms). */
#define SRL_TOUT 20 /* Idle time between serial frames(ms). */

/* D7-D2: Reserved(0).
* D1-D0: Baud rate select, 0=19200, 1=9600..., 3=2400. */
uint8_t ucBaud_g;

/* Const table for baud rate register. */
prog_char aucBdTbl_c[]=
{
UART_BAUD_SELECT(19200),
UART_BAUD_SELECT(9600),
UART_BAUD_SELECT(4800),
UART_BAUD_SELECT(2400),
};

/* Global flag. */
volatile uint8_t ucFlag_g;
#define RECV_ERR 0x01
#define RECV_FRM 0x02
#define SEND_MSG 0x04

#define RS485_TX_ON sbi(PORTC, 0)
#define RS485_TX_OFF cbi(PORTC, 0)

/* Serial buffer. */
uint8_t aucRcvBuf_g[MAX_RCV_LEN], aucSndBuf_g[MAX_SND_LEN];
/* Receive length, send length and now send position. */
volatile uint8_t ucRcvLen_g, ucSndLen_g, ucSndPos_g;
volatile uint8_t ucSrlTout_g; /* Frame time out counter. */

static void Deal_Recv_Msg(void);

int main(void)
{
volatile uint16_t unDelay;

/* Power on delay. */
for (ucBaud_g=0; ucBaud_g<10; ucBaud_g )
{
for (unDelay=0; unDelay<60000; unDelay )
continue;
}

/* Set timer 1 capture interrupt as system tick. */
TCCR1A=0x00;
TCCR1B=0x09;
OCR1AH=((F_CPU*TICK/1000) & 0xFF00)>>8;
OCR1AL=(F_CPU*TICK/1000) & 0xFF;
sbi(TIMSK, OCIE1A); /* Enable interrupt. */

ucBaud_g=eeprom_rb(0x10);

/* Default baud rate=9600. */
if (ucBaud_g>3)
ucBaud_g=1;
/* Set baud rate. */
UBRR=PRG_RDB(&aucBdTbl_c[ucBaud_g]);
/* Enable RxD/TxD and ints. */
UCR=(1<
sei(); /* Enable interrupts. */

while (1)
{
if (ucFlag_g & RECV_FRM)
{
Deal_Recv_Msg();
cli();
ucRcvLen_g=0;
ucFlag_g &= ~(RECV_FRM | RECV_ERR);
sei();
}
}
}

/* Signal handler for receive complete interrupt. */
SIGNAL(SIG_UART_RECV)
{
uint8_t ucFromUart;

ucFromUart=UDR; /* Read the Rx data first. */

/* Check if last command not deaLEDover. */
if ((ucFlag_g & RECV_FRM) || ucRcvLen_g>=MAX_RCV_LEN)
ucFlag_g |= RECV_ERR;
else if (!(ucFlag_g & SEND_MSG))
aucRcvBuf_g[ucRcvLen_g ]=ucFromUart;

ucFromUart=USR;
if (ucFromUart & ((0x01<ucFlag_g |= RECV_ERR;

ucSrlTout_g=SRL_TOUT/TICK; /* Reset the timeout counter. */
}

/* Signal handler for uart data buffer empty interrupt. */
SIGNAL(SIG_UART_DATA)
{
/* Write byte to data buffer. */
UDR=aucSndBuf_g[ucSndPos_g ];

if (ucSndPos_g>=ucSndLen_g) /* If buffer is empty: */
{
cbi(UCR, UDRIE); /* DISAble UDRIE interrupt. */
sbi(UCR, TXCIE);
}
}

SIGNAL(SIG_UART_TRANS)
{
cbi(UCR, TXCIE);
RS485_TX_OFF;
ucFlag_g &= ~SEND_MSG; /* Last message was dealed. */
}

SIGNAL(SIG_OUTPUT_COMPARE1A) /* Timer 1 output compare. */
{
if (ucSrlTout_g && !(--ucSrlTout_g))
ucFlag_g |= RECV_FRM;
}

static void Deal_Recv_Msg(void)
{
/* TODO: deal with receive message here. */
if (ucFlag_g & RECV_ERR)
return;
mEMCpy(aucSndBuf_g, aucRcvBuf_g, ucRcvLen_g);
ucSndLen_g=ucRcvLen_g;

if (ucSndLen_g>=5)
{
ucSndPos_g=0;

cli();

ucRcvLen_g=0;

ucFlag_g &= ~(RECV_FRM | RECV_ERR);
ucFlag_g |= SEND_MSG;

RS485_TX_ON;

sbi(UCR, UDRIE);

sei();
}
}


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

通過應(yīng)用R&S CMW500和R&S CMX500寬帶無線通信測(cè)試儀,Cooper General Global Services(簡(jiǎn)稱“Cooper General”)現(xiàn)已可以提供4G/LTE和 5G設(shè)備的支持和維修服...

關(guān)鍵字: rs 5G

羅德與施瓦茨近期簽訂了新的合同,將為菲律賓海岸警衛(wèi)隊(duì)的兩艘94米多用途快速反應(yīng)艇(MRRV)提供安全通信和無線電測(cè)向系統(tǒng)服務(wù)。

關(guān)鍵字: rs ntt

羅德與施瓦茨公司生產(chǎn)的ARDRONIS系統(tǒng)能夠檢測(cè)和識(shí)別無人機(jī)遙控信號(hào),并且能夠測(cè)量無人機(jī)遙控信號(hào)的來波方向并阻斷該信號(hào),從而阻止無人機(jī)悄無聲息的抵近目標(biāo)區(qū)域。

關(guān)鍵字: rs ardronis

  中國(guó) 北京,2015年8月27日 – 服務(wù)于全球工程師的分銷商Electrocomponents plc (LSE:ECM)集團(tuán)旗下的貿(mào)易品牌RS Components (RS)

關(guān)鍵字: rs

  中國(guó)北京,2015年10月22日-服務(wù)于全球工程師的分銷商Electrocomponents plc (LSE:ECM)集團(tuán)旗下的貿(mào)易品牌RS Components (RS)公司宣布,推出W

關(guān)鍵字: rs 物聯(lián)網(wǎng) 6lowpan網(wǎng)

  中國(guó)北京,2016年1月6日- 服務(wù)于全球工程師的分銷商Electrocomponents plc (LSE:ECM)集團(tuán)旗下的貿(mào)易品牌RS Components (RS)公司宣布推出Arr

關(guān)鍵字: rs 物聯(lián)網(wǎng)

  中國(guó)北京,2016年2月23日 - 服務(wù)于全球工程師的分銷商Electrocomponents plc (LSE:ECM) 集團(tuán)旗下的貿(mào)易品牌RS Components (RS)公司推出最新

關(guān)鍵字: rs 微控制器 物聯(lián)網(wǎng)

盡管無線網(wǎng)絡(luò)越來越普及,有線串行網(wǎng)絡(luò)依然是最可靠、魯棒性最好的通信方式,特別是在惡劣環(huán)境下。對(duì)于工業(yè)和樓宇自動(dòng)化等要求具備較高的抗干擾、抗靜電能力,以及高壓故障保護(hù)的應(yīng)用環(huán)境,這類網(wǎng)絡(luò)能夠提供更

關(guān)鍵字: rs

訓(xùn)練專項(xiàng)網(wǎng)絡(luò) 還記得我們?cè)陂_始時(shí)丟棄的70%的培訓(xùn)數(shù)據(jù)嗎?結(jié)果表明,如果我們想在Kaggle排行榜上獲得一個(gè)有競(jìng)爭(zhēng)力的得分,這是一個(gè)很糟糕的主意。在70%的數(shù)據(jù)和挑戰(zhàn)的測(cè)試集中,我們的模

關(guān)鍵字: rs 通信協(xié)議

什么是新型毫米波雷達(dá)傳感器技術(shù)?它有什么特點(diǎn)?測(cè)試與測(cè)量領(lǐng)域的先行者羅德與施瓦茨(Rohde & Schwarz)為脈沖相干雷達(dá)(PCR)和物聯(lián)網(wǎng)領(lǐng)域的先行者Acconeer提供了R&S FSW85信號(hào)和頻譜分析儀。Ac...

關(guān)鍵字: acconeer rs 雷達(dá)傳感器
關(guān)閉