文章目錄
- 前言
- 中斷的方式發送數據
- 代碼示例
- 準備工作
- 宏定義及全局變量
- 初始化
- 中斷服務函數
- ==發送邏輯函數==
- 啟動發送函數
 
- 總結
前言
 
關于賽元單片機觸摸的那篇文章確實幫助到過一些網友,后來有網友私信說賽元單片機的三合一串口功能遇到了問題,考慮到我之前的項目中用到過這個串口,也調通了,便想寫下這篇文章,目的是幫助遇到問題的網友,以及記錄學習的過程。
 所用單片機型號:SC92F8463B(同系列的都可以參考本篇文章),主頻:12Mhz
 之前用過STM32F103C8T6的單片機,在那次的使用經歷中,第一次了解并學習到了串口的使用方法,當時的項目中,參考的是原子哥的示例代碼進行串口的收發,后面測試的過程中,發現串口功能會影響到其它的功能。在老工程師的指導下,才了解到,原子哥的串口發送方式是不太適用于工作中的項目的,原因也很簡單,以阻塞的方式發送數據幀,再說直白一點,通過while()查詢標志位以此完成數據的發送。
 因此,我便想先介紹一種不一樣的數據發送方式!
 
中斷的方式發送數據
 
使用串口時,經常會用到以中斷的方式接受數據,但很少使用以中斷的方式發送數據,網上相關的資料也很少。但是這是一種非常好的方式,特別是在實際的工作中,會更加深刻的體會到這一點。而且不論是51單片機還是基于ARM的單片機,我所使用過的單片機都能以中斷的方式發送數據。
 
先來看看SC92F846XB的規格書
 
 
 
 PS:這里我截的是UART0的相關寄存器,而不是三合一串口的,因為三合一串口沒有解釋的這么清楚,但是我在實際使用時,發現三合一串口和UART0一樣,都沒什么區別,一樣有發送中斷的功能,只是寄存器不一樣而已。
 
 
先看第一幅圖中紅框里的內容:發送和接收完成時可產生中斷RI/TI,該中斷標志需要軟件清除。
 接收就不說了,用過串口的都清楚這個方式,主要說發送,我來大致描述一下以中斷發送數據這個過程:
 
#mermaid-svg-swZCRECQUNKe7PXI .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-swZCRECQUNKe7PXI .label text{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .node rect,#mermaid-svg-swZCRECQUNKe7PXI .node circle,#mermaid-svg-swZCRECQUNKe7PXI .node ellipse,#mermaid-svg-swZCRECQUNKe7PXI .node polygon,#mermaid-svg-swZCRECQUNKe7PXI .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-swZCRECQUNKe7PXI .node .label{text-align:center;fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .node.clickable{cursor:pointer}#mermaid-svg-swZCRECQUNKe7PXI .arrowheadPath{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-swZCRECQUNKe7PXI .flowchart-link{stroke:#333;fill:none}#mermaid-svg-swZCRECQUNKe7PXI .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-swZCRECQUNKe7PXI .edgeLabel rect{opacity:0.9}#mermaid-svg-swZCRECQUNKe7PXI .edgeLabel span{color:#333}#mermaid-svg-swZCRECQUNKe7PXI .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-swZCRECQUNKe7PXI .cluster text{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-swZCRECQUNKe7PXI .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-swZCRECQUNKe7PXI text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-swZCRECQUNKe7PXI .actor-line{stroke:grey}#mermaid-svg-swZCRECQUNKe7PXI .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-swZCRECQUNKe7PXI .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-swZCRECQUNKe7PXI #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-swZCRECQUNKe7PXI .sequenceNumber{fill:#fff}#mermaid-svg-swZCRECQUNKe7PXI #sequencenumber{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI #crosshead path{fill:#333;stroke:#333}#mermaid-svg-swZCRECQUNKe7PXI .messageText{fill:#333;stroke:#333}#mermaid-svg-swZCRECQUNKe7PXI .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-swZCRECQUNKe7PXI .labelText,#mermaid-svg-swZCRECQUNKe7PXI .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-swZCRECQUNKe7PXI .loopText,#mermaid-svg-swZCRECQUNKe7PXI .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-swZCRECQUNKe7PXI .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-swZCRECQUNKe7PXI .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-swZCRECQUNKe7PXI .noteText,#mermaid-svg-swZCRECQUNKe7PXI .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-swZCRECQUNKe7PXI .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-swZCRECQUNKe7PXI .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-swZCRECQUNKe7PXI .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-swZCRECQUNKe7PXI .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .section{stroke:none;opacity:0.2}#mermaid-svg-swZCRECQUNKe7PXI .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-swZCRECQUNKe7PXI .section2{fill:#fff400}#mermaid-svg-swZCRECQUNKe7PXI .section1,#mermaid-svg-swZCRECQUNKe7PXI .section3{fill:#fff;opacity:0.2}#mermaid-svg-swZCRECQUNKe7PXI .sectionTitle0{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .sectionTitle1{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .sectionTitle2{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .sectionTitle3{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-swZCRECQUNKe7PXI .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .grid path{stroke-width:0}#mermaid-svg-swZCRECQUNKe7PXI .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-swZCRECQUNKe7PXI .task{stroke-width:2}#mermaid-svg-swZCRECQUNKe7PXI .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .taskText:not([font-size]){font-size:11px}#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-swZCRECQUNKe7PXI .task.clickable{cursor:pointer}#mermaid-svg-swZCRECQUNKe7PXI .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-swZCRECQUNKe7PXI .taskText0,#mermaid-svg-swZCRECQUNKe7PXI .taskText1,#mermaid-svg-swZCRECQUNKe7PXI .taskText2,#mermaid-svg-swZCRECQUNKe7PXI .taskText3{fill:#fff}#mermaid-svg-swZCRECQUNKe7PXI .task0,#mermaid-svg-swZCRECQUNKe7PXI .task1,#mermaid-svg-swZCRECQUNKe7PXI .task2,#mermaid-svg-swZCRECQUNKe7PXI .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutside0,#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutside2{fill:#000}#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutside1,#mermaid-svg-swZCRECQUNKe7PXI .taskTextOutside3{fill:#000}#mermaid-svg-swZCRECQUNKe7PXI .active0,#mermaid-svg-swZCRECQUNKe7PXI .active1,#mermaid-svg-swZCRECQUNKe7PXI .active2,#mermaid-svg-swZCRECQUNKe7PXI .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-swZCRECQUNKe7PXI .activeText0,#mermaid-svg-swZCRECQUNKe7PXI .activeText1,#mermaid-svg-swZCRECQUNKe7PXI .activeText2,#mermaid-svg-swZCRECQUNKe7PXI .activeText3{fill:#000 !important}#mermaid-svg-swZCRECQUNKe7PXI .done0,#mermaid-svg-swZCRECQUNKe7PXI .done1,#mermaid-svg-swZCRECQUNKe7PXI .done2,#mermaid-svg-swZCRECQUNKe7PXI .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-swZCRECQUNKe7PXI .doneText0,#mermaid-svg-swZCRECQUNKe7PXI .doneText1,#mermaid-svg-swZCRECQUNKe7PXI .doneText2,#mermaid-svg-swZCRECQUNKe7PXI .doneText3{fill:#000 !important}#mermaid-svg-swZCRECQUNKe7PXI .crit0,#mermaid-svg-swZCRECQUNKe7PXI .crit1,#mermaid-svg-swZCRECQUNKe7PXI .crit2,#mermaid-svg-swZCRECQUNKe7PXI .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-swZCRECQUNKe7PXI .activeCrit0,#mermaid-svg-swZCRECQUNKe7PXI .activeCrit1,#mermaid-svg-swZCRECQUNKe7PXI .activeCrit2,#mermaid-svg-swZCRECQUNKe7PXI .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-swZCRECQUNKe7PXI .doneCrit0,#mermaid-svg-swZCRECQUNKe7PXI .doneCrit1,#mermaid-svg-swZCRECQUNKe7PXI .doneCrit2,#mermaid-svg-swZCRECQUNKe7PXI .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-swZCRECQUNKe7PXI .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-swZCRECQUNKe7PXI .milestoneText{font-style:italic}#mermaid-svg-swZCRECQUNKe7PXI .doneCritText0,#mermaid-svg-swZCRECQUNKe7PXI .doneCritText1,#mermaid-svg-swZCRECQUNKe7PXI .doneCritText2,#mermaid-svg-swZCRECQUNKe7PXI .doneCritText3{fill:#000 !important}#mermaid-svg-swZCRECQUNKe7PXI .activeCritText0,#mermaid-svg-swZCRECQUNKe7PXI .activeCritText1,#mermaid-svg-swZCRECQUNKe7PXI .activeCritText2,#mermaid-svg-swZCRECQUNKe7PXI .activeCritText3{fill:#000 !important}#mermaid-svg-swZCRECQUNKe7PXI .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-swZCRECQUNKe7PXI g.classGroup text .title{font-weight:bolder}#mermaid-svg-swZCRECQUNKe7PXI g.clickable{cursor:pointer}#mermaid-svg-swZCRECQUNKe7PXI g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-swZCRECQUNKe7PXI g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-swZCRECQUNKe7PXI .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-swZCRECQUNKe7PXI .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-swZCRECQUNKe7PXI .dashed-line{stroke-dasharray:3}#mermaid-svg-swZCRECQUNKe7PXI #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI .commit-id,#mermaid-svg-swZCRECQUNKe7PXI .commit-msg,#mermaid-svg-swZCRECQUNKe7PXI .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-swZCRECQUNKe7PXI g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-swZCRECQUNKe7PXI g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-swZCRECQUNKe7PXI g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-swZCRECQUNKe7PXI .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-swZCRECQUNKe7PXI .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-swZCRECQUNKe7PXI .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-swZCRECQUNKe7PXI .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-swZCRECQUNKe7PXI .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-swZCRECQUNKe7PXI .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-swZCRECQUNKe7PXI .edgeLabel text{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-swZCRECQUNKe7PXI .node circle.state-start{fill:black;stroke:black}#mermaid-svg-swZCRECQUNKe7PXI .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-swZCRECQUNKe7PXI #statediagram-barbEnd{fill:#9370db}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-state .divider{stroke:#9370db}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-swZCRECQUNKe7PXI .note-edge{stroke-dasharray:5}#mermaid-svg-swZCRECQUNKe7PXI .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-swZCRECQUNKe7PXI .error-icon{fill:#522}#mermaid-svg-swZCRECQUNKe7PXI .error-text{fill:#522;stroke:#522}#mermaid-svg-swZCRECQUNKe7PXI .edge-thickness-normal{stroke-width:2px}#mermaid-svg-swZCRECQUNKe7PXI .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-swZCRECQUNKe7PXI .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-swZCRECQUNKe7PXI .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-swZCRECQUNKe7PXI .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-swZCRECQUNKe7PXI .marker{fill:#333}#mermaid-svg-swZCRECQUNKe7PXI .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-swZCRECQUNKe7PXI {color: rgba(0, 0, 0, 0.75);font: ;}
1.向串口數據寄存器寫入數據2.物理層面發送數據,軟件執行其它任務3.物理層面發送完畢,TI置位,進入發送中斷服務函數4.TI標志位清0,由內部邏輯決定繼續發送或者停止發送
 
整個發送的過程其實非常簡單和順暢,而且單片機的效率也能達到最高。
 再來對比下查詢的方式進行發送:
 
#mermaid-svg-VKYVZL7ClDmbEjGk .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .label text{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .node rect,#mermaid-svg-VKYVZL7ClDmbEjGk .node circle,#mermaid-svg-VKYVZL7ClDmbEjGk .node ellipse,#mermaid-svg-VKYVZL7ClDmbEjGk .node polygon,#mermaid-svg-VKYVZL7ClDmbEjGk .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-VKYVZL7ClDmbEjGk .node .label{text-align:center;fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .node.clickable{cursor:pointer}#mermaid-svg-VKYVZL7ClDmbEjGk .arrowheadPath{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-VKYVZL7ClDmbEjGk .flowchart-link{stroke:#333;fill:none}#mermaid-svg-VKYVZL7ClDmbEjGk .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-VKYVZL7ClDmbEjGk .edgeLabel rect{opacity:0.9}#mermaid-svg-VKYVZL7ClDmbEjGk .edgeLabel span{color:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-VKYVZL7ClDmbEjGk .cluster text{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-VKYVZL7ClDmbEjGk .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-VKYVZL7ClDmbEjGk text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-VKYVZL7ClDmbEjGk .actor-line{stroke:grey}#mermaid-svg-VKYVZL7ClDmbEjGk .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-VKYVZL7ClDmbEjGk #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .sequenceNumber{fill:#fff}#mermaid-svg-VKYVZL7ClDmbEjGk #sequencenumber{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk #crosshead path{fill:#333;stroke:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .messageText{fill:#333;stroke:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-VKYVZL7ClDmbEjGk .labelText,#mermaid-svg-VKYVZL7ClDmbEjGk .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-VKYVZL7ClDmbEjGk .loopText,#mermaid-svg-VKYVZL7ClDmbEjGk .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-VKYVZL7ClDmbEjGk .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-VKYVZL7ClDmbEjGk .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-VKYVZL7ClDmbEjGk .noteText,#mermaid-svg-VKYVZL7ClDmbEjGk .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-VKYVZL7ClDmbEjGk .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-VKYVZL7ClDmbEjGk .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-VKYVZL7ClDmbEjGk .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-VKYVZL7ClDmbEjGk .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .section{stroke:none;opacity:0.2}#mermaid-svg-VKYVZL7ClDmbEjGk .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-VKYVZL7ClDmbEjGk .section2{fill:#fff400}#mermaid-svg-VKYVZL7ClDmbEjGk .section1,#mermaid-svg-VKYVZL7ClDmbEjGk .section3{fill:#fff;opacity:0.2}#mermaid-svg-VKYVZL7ClDmbEjGk .sectionTitle0{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .sectionTitle1{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .sectionTitle2{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .sectionTitle3{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-VKYVZL7ClDmbEjGk .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .grid path{stroke-width:0}#mermaid-svg-VKYVZL7ClDmbEjGk .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-VKYVZL7ClDmbEjGk .task{stroke-width:2}#mermaid-svg-VKYVZL7ClDmbEjGk .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .taskText:not([font-size]){font-size:11px}#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-VKYVZL7ClDmbEjGk .task.clickable{cursor:pointer}#mermaid-svg-VKYVZL7ClDmbEjGk .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-VKYVZL7ClDmbEjGk .taskText0,#mermaid-svg-VKYVZL7ClDmbEjGk .taskText1,#mermaid-svg-VKYVZL7ClDmbEjGk .taskText2,#mermaid-svg-VKYVZL7ClDmbEjGk .taskText3{fill:#fff}#mermaid-svg-VKYVZL7ClDmbEjGk .task0,#mermaid-svg-VKYVZL7ClDmbEjGk .task1,#mermaid-svg-VKYVZL7ClDmbEjGk .task2,#mermaid-svg-VKYVZL7ClDmbEjGk .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutside0,#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutside2{fill:#000}#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutside1,#mermaid-svg-VKYVZL7ClDmbEjGk .taskTextOutside3{fill:#000}#mermaid-svg-VKYVZL7ClDmbEjGk .active0,#mermaid-svg-VKYVZL7ClDmbEjGk .active1,#mermaid-svg-VKYVZL7ClDmbEjGk .active2,#mermaid-svg-VKYVZL7ClDmbEjGk .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-VKYVZL7ClDmbEjGk .activeText0,#mermaid-svg-VKYVZL7ClDmbEjGk .activeText1,#mermaid-svg-VKYVZL7ClDmbEjGk .activeText2,#mermaid-svg-VKYVZL7ClDmbEjGk .activeText3{fill:#000 !important}#mermaid-svg-VKYVZL7ClDmbEjGk .done0,#mermaid-svg-VKYVZL7ClDmbEjGk .done1,#mermaid-svg-VKYVZL7ClDmbEjGk .done2,#mermaid-svg-VKYVZL7ClDmbEjGk .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-VKYVZL7ClDmbEjGk .doneText0,#mermaid-svg-VKYVZL7ClDmbEjGk .doneText1,#mermaid-svg-VKYVZL7ClDmbEjGk .doneText2,#mermaid-svg-VKYVZL7ClDmbEjGk .doneText3{fill:#000 !important}#mermaid-svg-VKYVZL7ClDmbEjGk .crit0,#mermaid-svg-VKYVZL7ClDmbEjGk .crit1,#mermaid-svg-VKYVZL7ClDmbEjGk .crit2,#mermaid-svg-VKYVZL7ClDmbEjGk .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-VKYVZL7ClDmbEjGk .activeCrit0,#mermaid-svg-VKYVZL7ClDmbEjGk .activeCrit1,#mermaid-svg-VKYVZL7ClDmbEjGk .activeCrit2,#mermaid-svg-VKYVZL7ClDmbEjGk .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-VKYVZL7ClDmbEjGk .doneCrit0,#mermaid-svg-VKYVZL7ClDmbEjGk .doneCrit1,#mermaid-svg-VKYVZL7ClDmbEjGk .doneCrit2,#mermaid-svg-VKYVZL7ClDmbEjGk .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-VKYVZL7ClDmbEjGk .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-VKYVZL7ClDmbEjGk .milestoneText{font-style:italic}#mermaid-svg-VKYVZL7ClDmbEjGk .doneCritText0,#mermaid-svg-VKYVZL7ClDmbEjGk .doneCritText1,#mermaid-svg-VKYVZL7ClDmbEjGk .doneCritText2,#mermaid-svg-VKYVZL7ClDmbEjGk .doneCritText3{fill:#000 !important}#mermaid-svg-VKYVZL7ClDmbEjGk .activeCritText0,#mermaid-svg-VKYVZL7ClDmbEjGk .activeCritText1,#mermaid-svg-VKYVZL7ClDmbEjGk .activeCritText2,#mermaid-svg-VKYVZL7ClDmbEjGk .activeCritText3{fill:#000 !important}#mermaid-svg-VKYVZL7ClDmbEjGk .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-VKYVZL7ClDmbEjGk g.classGroup text .title{font-weight:bolder}#mermaid-svg-VKYVZL7ClDmbEjGk g.clickable{cursor:pointer}#mermaid-svg-VKYVZL7ClDmbEjGk g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-VKYVZL7ClDmbEjGk g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-VKYVZL7ClDmbEjGk .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-VKYVZL7ClDmbEjGk .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-VKYVZL7ClDmbEjGk .dashed-line{stroke-dasharray:3}#mermaid-svg-VKYVZL7ClDmbEjGk #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk .commit-id,#mermaid-svg-VKYVZL7ClDmbEjGk .commit-msg,#mermaid-svg-VKYVZL7ClDmbEjGk .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-VKYVZL7ClDmbEjGk g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-VKYVZL7ClDmbEjGk g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-VKYVZL7ClDmbEjGk g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-VKYVZL7ClDmbEjGk .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-VKYVZL7ClDmbEjGk .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-VKYVZL7ClDmbEjGk .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-VKYVZL7ClDmbEjGk .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-VKYVZL7ClDmbEjGk .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-VKYVZL7ClDmbEjGk .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-VKYVZL7ClDmbEjGk .edgeLabel text{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-VKYVZL7ClDmbEjGk .node circle.state-start{fill:black;stroke:black}#mermaid-svg-VKYVZL7ClDmbEjGk .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-VKYVZL7ClDmbEjGk #statediagram-barbEnd{fill:#9370db}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-state .divider{stroke:#9370db}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-VKYVZL7ClDmbEjGk .note-edge{stroke-dasharray:5}#mermaid-svg-VKYVZL7ClDmbEjGk .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-VKYVZL7ClDmbEjGk .error-icon{fill:#522}#mermaid-svg-VKYVZL7ClDmbEjGk .error-text{fill:#522;stroke:#522}#mermaid-svg-VKYVZL7ClDmbEjGk .edge-thickness-normal{stroke-width:2px}#mermaid-svg-VKYVZL7ClDmbEjGk .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-VKYVZL7ClDmbEjGk .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-VKYVZL7ClDmbEjGk .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-VKYVZL7ClDmbEjGk .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-VKYVZL7ClDmbEjGk .marker{fill:#333}#mermaid-svg-VKYVZL7ClDmbEjGk .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-VKYVZL7ClDmbEjGk {color: rgba(0, 0, 0, 0.75);font: ;}
1.向串口數據寄存器寫入數據2.物理層面發送數據,軟件阻塞在while中等待標志位置位3.標志位置位,由內部邏輯決定繼續發送或者停止發送
 
從流程圖中應該會比較容易看出這兩種發送方式的區別。采用中斷方式發送時,可以說全是優點,莫得缺點~
 
代碼示例
 
本文主要是為了解決網友的問題,再搞清楚中斷發送這件事后,以三合一串口為例,從初始化開始,到發送一幀數據結束,來完整的演示一下。
 
準備工作
 
前提條件:需要在main()函數中定時調用uart1_start_trans()函數,推薦500ms,1000ms。
 實驗現象:每間隔一次定時時長,會在串口助手中接收到data_for_tx中的數據。
 
宏定義及全局變量
 
#define SYSTEM_CLK      			((unsigned long int)12000000)
#define UART1_BAUD_9600_LOAD_VALUE	9600static u8 data_for_tx
[5] = {0x11, 0x22, 0x33, 0x44, 0x55};	
 
初始化
 
void hw_uart1_init(void)
{P2CON 
&= 0xFC;   											P2PH  
|= 0x03;OTCON 
|= 0xC0;    											SSCON0 
= 0x50;   											SSCON1 
= SYSTEM_CLK
/UART1_BAUD_9600_LOAD_VALUE
;   			SSCON2 
= (SYSTEM_CLK
/UART1_BAUD_9600_LOAD_VALUE
)>>8;   		IE1 
|= 0x01;      											IP1 
|= 0x01;												EA 
= 1;
}
 
這部分沒啥可說的,就是按照示例代碼和規格書寫的。
 
中斷服務函數
 
void uart1_isr(void)	interrupt 
7
{u8 temp_char 
= 0;if(SSCON0
&0x02)    					{SSCON0 
&= 0xFD;					uart1_send_data();				}if((SSCON0
&0x01))  					{SSCON0 
&= 0xFE;					temp_char 
= SSDAT
;uart1_receive_data(temp_char
);	}
}
 
中斷服務函數其實是很簡潔的,這里不再介紹接收邏輯的編寫,只說發送邏輯的函數,以中斷方式發送數據的重點也在這里~
 
發送邏輯函數
 
void uart1_send_data(void)
{static u8 index 
= 0;				if(index
>=5){index 
= 0;}else{SSDAT 
= data_for_tx
[index
++];}	
}
 
分析一下這個過程:index的初始值為0,在發送邏輯函數中,會將data_for_tx[0]寫入數據寄存器,物理層會發送data_for_tx[0],index變為1;發送完畢后,便會再次進入發送中斷函數,調用在發送邏輯函數時,會將data_for_tx[1]寫入數據寄存器,物理層會發送data_for_tx[1],index變為2,循環幾次后index變為5時,data_for_tx數組中的5個元素都已經發送完畢了(即這一幀數據發送完畢),這時已經不需要再發送了,需要做的僅僅是將index給清零,等待main函數中啟動下一幀數據的發送。
 以上是理解發送中斷的重點,一遍看不懂就多看幾遍,也可以自己動手畫一畫流程圖。
 
啟動發送函數
 
void uart1_start_trans(void)
{SSCON0 
|= 0x02;		
}
 
總結
 
以上便是此篇文章的全部內容,不僅僅是三合一串口的使用,更是介紹了中斷方式發送數據這種方式,掌握它,很有必要。
 如果本文對你有用,請點個贊吧,這是對我莫大的支持,當然也更歡迎評論區留言討論以及收藏哦~
 祝:變得更強!
                            總結
                            
                                以上是生活随笔為你收集整理的【串口发送中断】基于赛元单片机使用三合一串口的UART功能的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。