MAX7219级联显示时有一个模块总是有闪烁

2019-11-23 13:10发布

<pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"> &lt;div&gt;最近学习单片机,用的是<span class="hljs-number">877</span>A,<span class="hljs-number">4</span>M晶振,在用MAX7219级联驱动<span class="hljs-number">4</span>块<span class="hljs-number">8</span>*<span class="hljs-number">8</span>组成<span class="hljs-number">8</span>*<span class="hljs-number">32</span>点阵时,总是第三块有闪烁。请问是代码的问题吗?谢谢各位帮忙看一下,谢谢了!&lt;/div&gt;&lt;div&gt; &lt;/div&gt;&lt;div&gt;<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;pic.h&gt;</span></span> &lt;/div&gt;__CONFIG(HS&amp;WDTDIS&amp;LVPDIS); <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> INT8U unsigned char</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> _XTAL_FREQ 4000000UL</span> <span class="hljs-comment">//定义Max7219端口</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> DIN RB0</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> CS RB1</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> CLK RB2</span> <span class="hljs-keyword">const</span> INT8U disp1[<span class="hljs-number">10</span>][<span class="hljs-number">8</span>]={ {<span class="hljs-number">0x3C</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3C</span>},<span class="hljs-comment">//0</span> {<span class="hljs-number">0x18</span>,<span class="hljs-number">0x28</span>,<span class="hljs-number">0x08</span>,<span class="hljs-number">0x08</span>,<span class="hljs-number">0x08</span>,<span class="hljs-number">0x08</span>,<span class="hljs-number">0x08</span>,<span class="hljs-number">0x08</span>},<span class="hljs-comment">//1</span> {<span class="hljs-number">0x3c</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x02</span>,<span class="hljs-number">0x1c</span>,<span class="hljs-number">0x20</span>,<span class="hljs-number">0x40</span>,<span class="hljs-number">0x7E</span>},<span class="hljs-comment">//2</span> {<span class="hljs-number">0x3c</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x0c</span>,<span class="hljs-number">0x2</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3c</span>},<span class="hljs-comment">//3</span> {<span class="hljs-number">0x28</span>,<span class="hljs-number">0x28</span>,<span class="hljs-number">0x48</span>,<span class="hljs-number">0x48</span>,<span class="hljs-number">0x7e</span>,<span class="hljs-number">0x8</span>,<span class="hljs-number">0x8</span>,<span class="hljs-number">0x8</span>},<span class="hljs-comment">//4</span> {<span class="hljs-number">0x3e</span>,<span class="hljs-number">0x40</span>,<span class="hljs-number">0x40</span>,<span class="hljs-number">0x7c</span>,<span class="hljs-number">0x2</span>,<span class="hljs-number">0x2</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3c</span>},<span class="hljs-comment">//5</span> {<span class="hljs-number">0x3e</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x40</span>,<span class="hljs-number">0x7C</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3c</span>},<span class="hljs-comment">//6</span> {<span class="hljs-number">0x7E</span>,<span class="hljs-number">0x02</span>,<span class="hljs-number">0x4</span>,<span class="hljs-number">0x8</span>,<span class="hljs-number">0x8</span>,<span class="hljs-number">0x8</span>,<span class="hljs-number">0x8</span>,<span class="hljs-number">0x8</span>},<span class="hljs-comment">//7</span> {<span class="hljs-number">0x3c</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3c</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3c</span>},<span class="hljs-comment">//8</span> {<span class="hljs-number">0x3c</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3e</span>,<span class="hljs-number">0x2</span>,<span class="hljs-number">0x42</span>,<span class="hljs-number">0x3c</span>}<span class="hljs-comment">//9</span> }; <span class="hljs-comment">//-----------------------------------------------------------------------------</span> <span class="hljs-comment">//向MAX7219写入字节</span> <span class="hljs-comment">//-----------------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Write_Max7219_byte</span><span class="hljs-params">(INT8U DATA)</span> </span>{ INT8U i; CS=<span class="hljs-number">0</span>; <span class="hljs-comment">//CS=0有效,CS=1锁存 </span> <span class="hljs-keyword">for</span>(i=<span class="hljs-number">8</span>;i&gt;=<span class="hljs-number">1</span>;i--) { CLK=<span class="hljs-number">0</span>; IF(DATA &amp; <span class="hljs-number">0x80</span>) DIN=<span class="hljs-number">1</span>; <span class="hljs-keyword">else</span> DIN=<span class="hljs-number">0</span>; <span class="hljs-comment">//DIN=(DATA&amp;0x80); //&amp;10000000, 编译器对位操作的理解:非0即为1</span> DATA=DATA&lt;&lt;<span class="hljs-number">1</span>; CLK=<span class="hljs-number">1</span>; <span class="hljs-comment">//上升沿把数据送出去</span> } } <span class="hljs-comment">//------------------------------------------------------------------</span> <span class="hljs-comment">//向M第一片MAX7219写入数据</span> <span class="hljs-comment">//------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Write_Max7219_1</span><span class="hljs-params">(INT8U add1,INT8U dat1)</span> </span>{ CS=<span class="hljs-number">0</span>; Write_Max7219_byte(add1); <span class="hljs-comment">//写入地址,即数码管编号</span> Write_Max7219_byte(dat1); <span class="hljs-comment">//写入数据,即数码管显示数字 </span> CS=<span class="hljs-number">1</span>; } <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-comment">//第二片MAX7219的写入数据</span> <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Write_Max7219_2</span><span class="hljs-params">(INT8U add2,INT8U dat2)</span> </span>{ CS=<span class="hljs-number">0</span>; Write_Max7219_byte(add2); Write_Max7219_byte(dat2); CLK=<span class="hljs-number">1</span>; Write_Max7219_byte(<span class="hljs-number">0x00</span>); <span class="hljs-comment">//片1写入空</span> Write_Max7219_byte(<span class="hljs-number">0x00</span>); CS=<span class="hljs-number">1</span>; } <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-comment">//第三片MAX7219的写入数据</span> <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Write_Max7219_3</span><span class="hljs-params">(INT8U add3,INT8U dat3)</span> </span>{ CS=<span class="hljs-number">0</span>; Write_Max7219_byte(add3); Write_Max7219_byte(dat3); CLK=<span class="hljs-number">1</span>; Write_Max7219_byte(<span class="hljs-number">0x00</span>); <span class="hljs-comment">//片1写入空</span> Write_Max7219_byte(<span class="hljs-number">0x00</span>); Write_Max7219_byte(<span class="hljs-number">0x00</span>); <span class="hljs-comment">//片2写入空</span> Write_Max7219_byte(<span class="hljs-number">0x00</span>); CS=<span class="hljs-number">1</span>; } <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-comment">//第四片MAX7219的写入数据</span> <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Write_Max7219_4</span><span class="hljs-params">(INT8U add4,INT8U dat4)</span> </span>{ CS=<span class="hljs-number">0</span>; Write_Max7219_byte(add4); Write_Max7219_byte(dat4); CLK=<span class="hljs-number">1</span>; Write_Max7219_byte(<span class="hljs-number">0x00</span>); <span class="hljs-comment">//片1写入空</span> Write_Max7219_byte(<span class="hljs-number">0x00</span>); Write_Max7219_byte(<span class="hljs-number">0x00</span>); <span class="hljs-comment">//片2写入空</span> Write_Max7219_byte(<span class="hljs-number">0x00</span>); Write_Max7219_byte(<span class="hljs-number">0x00</span>); <span class="hljs-comment">//片3写入空</span> Write_Max7219_byte(<span class="hljs-number">0x00</span>); CS=<span class="hljs-number">1</span>; } <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-comment">//初始化芯片</span> <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Init_MAX7219</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{ Write_Max7219_1(<span class="hljs-number">0x09</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//译码方式:不译码</span> Write_Max7219_1(<span class="hljs-number">0x0a</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//亮度最小 </span> Write_Max7219_1(<span class="hljs-number">0x0b</span>, <span class="hljs-number">0x07</span>); <span class="hljs-comment">//扫描界限;8个数码管显示</span> Write_Max7219_1(<span class="hljs-number">0x0c</span>, <span class="hljs-number">0x01</span>); <span class="hljs-comment">//掉电模式:0,普通模式:1</span> Write_Max7219_1(<span class="hljs-number">0x0f</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//显示测试:1;测试结束,正常显示:0</span> Write_Max7219_2(<span class="hljs-number">0x09</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//译码方式:不译码</span> Write_Max7219_2(<span class="hljs-number">0x0a</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//亮度最小 </span> Write_Max7219_2(<span class="hljs-number">0x0b</span>, <span class="hljs-number">0x07</span>); <span class="hljs-comment">//扫描界限;8个数码管显示</span> Write_Max7219_2(<span class="hljs-number">0x0c</span>, <span class="hljs-number">0x01</span>); <span class="hljs-comment">//掉电模式:0,普通模式:1</span> Write_Max7219_2(<span class="hljs-number">0x0f</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//显示测试:1;测试结束,正常显示:0</span> Write_Max7219_3(<span class="hljs-number">0x09</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//译码方式:不译码</span> Write_Max7219_3(<span class="hljs-number">0x0a</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//亮度最小 </span> Write_Max7219_3(<span class="hljs-number">0x0b</span>, <span class="hljs-number">0x07</span>); <span class="hljs-comment">//扫描界限;8个数码管显示</span> Write_Max7219_3(<span class="hljs-number">0x0c</span>, <span class="hljs-number">0x01</span>); <span class="hljs-comment">//掉电模式:0,普通模式:1</span> Write_Max7219_3(<span class="hljs-number">0x0f</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//显示测试:1;测试结束,正常显示:0</span> Write_Max7219_4(<span class="hljs-number">0x09</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//译码方式:不译码</span> Write_Max7219_4(<span class="hljs-number">0x0a</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//亮度最小 </span> Write_Max7219_4(<span class="hljs-number">0x0b</span>, <span class="hljs-number">0x07</span>); <span class="hljs-comment">//扫描界限;8个数码管显示</span> Write_Max7219_4(<span class="hljs-number">0x0c</span>, <span class="hljs-number">0x01</span>); <span class="hljs-comment">//掉电模式:0,普通模式:1</span> Write_Max7219_4(<span class="hljs-number">0x0f</span>, <span class="hljs-number">0x00</span>); <span class="hljs-comment">//显示测试:1;测试结束,正常显示:</span> } <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-comment">//*********************主程序**************************</span> <span class="hljs-comment">//---------------------------------------------------------------------</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{ INT8U i,j=<span class="hljs-number">0</span>,k=<span class="hljs-number">0</span>;; TRISB=<span class="hljs-number">0</span>; PORTB=<span class="hljs-number">0x00</span>; __delay_ms(<span class="hljs-number">50</span>); Init_MAX7219(); <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) { <span class="hljs-keyword">for</span>(i=<span class="hljs-number">1</span>;i&lt;<span class="hljs-number">9</span>;i++) { Write_Max7219_1(i,disp1[j%<span class="hljs-number">10</span>][i<span class="hljs-number">-1</span>]); Write_Max7219_2(i,disp1[j/<span class="hljs-number">10</span>%<span class="hljs-number">10</span>][i<span class="hljs-number">-1</span>]); Write_Max7219_3(i,disp1[j/<span class="hljs-number">100</span>%<span class="hljs-number">10</span>][i<span class="hljs-number">-1</span>]); Write_Max7219_4(i,disp1[k][i<span class="hljs-number">-1</span>]); } j+=<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span>(j&gt;<span class="hljs-number">254</span>) { j=<span class="hljs-number">0</span>;k+=<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span>(k&gt;<span class="hljs-number">9</span>) k=<span class="hljs-number">0</span>; } __delay_ms(<span class="hljs-number">300</span>); } }</code></pre> <p><img src="https://image.xiaopingtou.net/data/attach/191123/4JeX53EL.jpg" alt="aaa"><img src="https://image.xiaopingtou.net/data/attach/191123/WPTiVcF3.jpg" alt="bbb"><img src="https://image.xiaopingtou.net/data/attach/191123/h9unku7j.jpg" alt="ccc"><br></p><p><br></p>