当前位置:   article > 正文

基于FPGA的频率计与串口通信(二)

基于FPGA的频率计与串口通信(二)

接上篇,本文章展示基于FPGA的频率计与串口通信项目部分核心代码。

顶层文件

top.bdf

  1. //#pragma file_not_in_maxplusii_format
  2. (header "graphic" (version "1.3"))
  3. (pin
  4. (input)
  5. (rect 80 328 248 344)
  6. (text "INPUT" (rect 133 0 161 10)(font "Arial" (font_size 6)))
  7. (text "clk" (rect 5 0 19 12)(font "Arial" ))
  8. (pt 168 8)
  9. (drawing
  10. (line (pt 92 12)(pt 117 12)(line_width 1))
  11. (line (pt 92 4)(pt 117 4)(line_width 1))
  12. (line (pt 121 8)(pt 168 8)(line_width 1))
  13. (line (pt 92 12)(pt 92 4)(line_width 1))
  14. (line (pt 117 4)(pt 121 8)(line_width 1))
  15. (line (pt 117 12)(pt 121 8)(line_width 1))
  16. )
  17. (text "VCC" (rect 136 7 156 17)(font "Arial" (font_size 6)))
  18. (annotation_block (location)(rect 32 328 80 344))
  19. )
  20. (pin
  21. (input)
  22. (rect 80 344 248 360)
  23. (text "INPUT" (rect 133 0 161 10)(font "Arial" (font_size 6)))
  24. (text "inclk" (rect 5 0 27 12)(font "Arial" ))
  25. (pt 168 8)
  26. (drawing
  27. (line (pt 92 12)(pt 117 12)(line_width 1))
  28. (line (pt 92 4)(pt 117 4)(line_width 1))
  29. (line (pt 121 8)(pt 168 8)(line_width 1))
  30. (line (pt 92 12)(pt 92 4)(line_width 1))
  31. (line (pt 117 4)(pt 121 8)(line_width 1))
  32. (line (pt 117 12)(pt 121 8)(line_width 1))
  33. )
  34. (text "VCC" (rect 136 7 156 17)(font "Arial" (font_size 6)))
  35. (annotation_block (location)(rect 32 344 80 360))
  36. )
  37. (pin
  38. (input)
  39. (rect 72 280 240 296)
  40. (text "INPUT" (rect 133 0 161 10)(font "Arial" (font_size 6)))
  41. (text "rst" (rect 5 0 17 12)(font "Arial" ))
  42. (pt 168 8)
  43. (drawing
  44. (line (pt 92 12)(pt 117 12)(line_width 1))
  45. (line (pt 92 4)(pt 117 4)(line_width 1))
  46. (line (pt 121 8)(pt 168 8)(line_width 1))
  47. (line (pt 92 12)(pt 92 4)(line_width 1))
  48. (line (pt 117 4)(pt 121 8)(line_width 1))
  49. (line (pt 117 12)(pt 121 8)(line_width 1))
  50. )
  51. (text "VCC" (rect 136 7 156 17)(font "Arial" (font_size 6)))
  52. (annotation_block (location)(rect 24 280 72 296))
  53. )
  54. (pin
  55. (input)
  56. (rect 32 376 200 392)
  57. (text "INPUT" (rect 133 0 161 10)(font "Arial" (font_size 6)))
  58. (text "key" (rect 5 0 23 12)(font "Arial" ))
  59. (pt 168 8)
  60. (drawing
  61. (line (pt 92 12)(pt 117 12)(line_width 1))
  62. (line (pt 92 4)(pt 117 4)(line_width 1))
  63. (line (pt 121 8)(pt 168 8)(line_width 1))
  64. (line (pt 92 12)(pt 92 4)(line_width 1))
  65. (line (pt 117 4)(pt 121 8)(line_width 1))
  66. (line (pt 117 12)(pt 121 8)(line_width 1))
  67. )
  68. (text "VCC" (rect 136 7 156 17)(font "Arial" (font_size 6)))
  69. (annotation_block (location)(rect -16 376 32 392))
  70. )
  71. (pin
  72. (output)
  73. (rect 872 312 1048 328)
  74. (text "OUTPUT" (rect 1 0 39 10)(font "Arial" (font_size 6)))
  75. (text "txd" (rect 90 0 105 12)(font "Arial" ))
  76. (pt 0 8)
  77. (drawing
  78. (line (pt 0 8)(pt 52 8)(line_width 1))
  79. (line (pt 52 4)(pt 78 4)(line_width 1))
  80. (line (pt 52 12)(pt 78 12)(line_width 1))
  81. (line (pt 52 12)(pt 52 4)(line_width 1))
  82. (line (pt 78 4)(pt 82 8)(line_width 1))
  83. (line (pt 82 8)(pt 78 12)(line_width 1))
  84. (line (pt 78 12)(pt 82 8)(line_width 1))
  85. )
  86. (annotation_block (location)(rect 1064 328 1112 344))
  87. )
  88. (pin
  89. (output)
  90. (rect 496 232 672 248)
  91. (text "OUTPUT" (rect 1 0 39 10)(font "Arial" (font_size 6)))
  92. (text "outclk" (rect 90 0 119 12)(font "Arial" ))
  93. (pt 0 8)
  94. (drawing
  95. (line (pt 0 8)(pt 52 8)(line_width 1))
  96. (line (pt 52 4)(pt 78 4)(line_width 1))
  97. (line (pt 52 12)(pt 78 12)(line_width 1))
  98. (line (pt 52 12)(pt 52 4)(line_width 1))
  99. (line (pt 78 4)(pt 82 8)(line_width 1))
  100. (line (pt 82 8)(pt 78 12)(line_width 1))
  101. (line (pt 78 12)(pt 82 8)(line_width 1))
  102. )
  103. (annotation_block (location)(rect 672 248 720 264))
  104. )
  105. (pin
  106. (output)
  107. (rect 512 168 688 184)
  108. (text "OUTPUT" (rect 1 0 39 10)(font "Arial" (font_size 6)))
  109. (text "seg_duan[7..0]" (rect 90 0 163 12)(font "Arial" ))
  110. (pt 0 8)
  111. (drawing
  112. (line (pt 0 8)(pt 52 8)(line_width 1))
  113. (line (pt 52 4)(pt 78 4)(line_width 1))
  114. (line (pt 52 12)(pt 78 12)(line_width 1))
  115. (line (pt 52 12)(pt 52 4)(line_width 1))
  116. (line (pt 78 4)(pt 82 8)(line_width 1))
  117. (line (pt 82 8)(pt 78 12)(line_width 1))
  118. (line (pt 78 12)(pt 82 8)(line_width 1))
  119. )
  120. (annotation_block (location)(rect 688 184 744 296))
  121. )
  122. (pin
  123. (output)
  124. (rect 536 200 712 216)
  125. (text "OUTPUT" (rect 1 0 39 10)(font "Arial" (font_size 6)))
  126. (text "seg_wei[7..0]" (rect 90 0 154 12)(font "Arial" ))
  127. (pt 0 8)
  128. (drawing
  129. (line (pt 0 8)(pt 52 8)(line_width 1))
  130. (line (pt 52 4)(pt 78 4)(line_width 1))
  131. (line (pt 52 12)(pt 78 12)(line_width 1))
  132. (line (pt 52 12)(pt 52 4)(line_width 1))
  133. (line (pt 78 4)(pt 82 8)(line_width 1))
  134. (line (pt 82 8)(pt 78 12)(line_width 1))
  135. (line (pt 78 12)(pt 82 8)(line_width 1))
  136. )
  137. (annotation_block (location)(rect 712 216 768 328))
  138. )
  139. (symbol
  140. (rect 648 288 824 544)
  141. (text "uart" (rect 5 0 23 12)(font "Arial" ))
  142. (text "inst" (rect 8 240 25 252)(font "Arial" ))
  143. (port
  144. (pt 0 32)
  145. (input)
  146. (text "clk" (rect 0 0 14 12)(font "Arial" ))
  147. (text "clk" (rect 21 27 35 39)(font "Arial" ))
  148. (line (pt 0 32)(pt 16 32)(line_width 1))
  149. )
  150. (port
  151. (pt 0 48)
  152. (input)
  153. (text "rst" (rect 0 0 12 12)(font "Arial" ))
  154. (text "rst" (rect 21 43 33 55)(font "Arial" ))
  155. (line (pt 0 48)(pt 16 48)(line_width 1))
  156. )
  157. (port
  158. (pt 0 64)
  159. (input)
  160. (text "rxd" (rect 0 0 15 12)(font "Arial" ))
  161. (text "rxd" (rect 21 59 36 71)(font "Arial" ))
  162. (line (pt 0 64)(pt 16 64)(line_width 1))
  163. )
  164. (port
  165. (pt 0 80)
  166. (input)
  167. (text "start" (rect 0 0 22 12)(font "Arial" ))
  168. (text "start" (rect 21 75 43 87)(font "Arial" ))
  169. (line (pt 0 80)(pt 16 80)(line_width 1))
  170. )
  171. (port
  172. (pt 0 96)
  173. (input)
  174. (text "data_cnt[3..0]" (rect 0 0 68 12)(font "Arial" ))
  175. (text "data_cnt[3..0]" (rect 21 91 89 103)(font "Arial" ))
  176. (line (pt 0 96)(pt 16 96)(line_width 3))
  177. )
  178. (port
  179. (pt 0 112)
  180. (input)
  181. (text "count1[7..0]" (rect 0 0 59 12)(font "Arial" ))
  182. (text "count1[7..0]" (rect 21 107 80 119)(font "Arial" ))
  183. (line (pt 0 112)(pt 16 112)(line_width 3))
  184. )
  185. (port
  186. (pt 0 128)
  187. (input)
  188. (text "count2[7..0]" (rect 0 0 59 12)(font "Arial" ))
  189. (text "count2[7..0]" (rect 21 123 80 135)(font "Arial" ))
  190. (line (pt 0 128)(pt 16 128)(line_width 3))
  191. )
  192. (port
  193. (pt 0 144)
  194. (input)
  195. (text "count3[7..0]" (rect 0 0 59 12)(font "Arial" ))
  196. (text "count3[7..0]" (rect 21 139 80 151)(font "Arial" ))
  197. (line (pt 0 144)(pt 16 144)(line_width 3))
  198. )
  199. (port
  200. (pt 0 160)
  201. (input)
  202. (text "count4[7..0]" (rect 0 0 59 12)(font "Arial" ))
  203. (text "count4[7..0]" (rect 21 155 80 167)(font "Arial" ))
  204. (line (pt 0 160)(pt 16 160)(line_width 3))
  205. )
  206. (port
  207. (pt 0 176)
  208. (input)
  209. (text "count5[7..0]" (rect 0 0 59 12)(font "Arial" ))
  210. (text "count5[7..0]" (rect 21 171 80 183)(font "Arial" ))
  211. (line (pt 0 176)(pt 16 176)(line_width 3))
  212. )
  213. (port
  214. (pt 0 192)
  215. (input)
  216. (text "count6[7..0]" (rect 0 0 59 12)(font "Arial" ))
  217. (text "count6[7..0]" (rect 21 187 80 199)(font "Arial" ))
  218. (line (pt 0 192)(pt 16 192)(line_width 3))
  219. )
  220. (port
  221. (pt 0 208)
  222. (input)
  223. (text "count7[7..0]" (rect 0 0 59 12)(font "Arial" ))
  224. (text "count7[7..0]" (rect 21 203 80 215)(font "Arial" ))
  225. (line (pt 0 208)(pt 16 208)(line_width 3))
  226. )
  227. (port
  228. (pt 0 224)
  229. (input)
  230. (text "count8[7..0]" (rect 0 0 59 12)(font "Arial" ))
  231. (text "count8[7..0]" (rect 21 219 80 231)(font "Arial" ))
  232. (line (pt 0 224)(pt 16 224)(line_width 3))
  233. )
  234. (port
  235. (pt 176 32)
  236. (output)
  237. (text "txd" (rect 0 0 15 12)(font "Arial" ))
  238. (text "txd" (rect 140 27 155 39)(font "Arial" ))
  239. (line (pt 176 32)(pt 160 32)(line_width 1))
  240. )
  241. (port
  242. (pt 176 48)
  243. (output)
  244. (text "send_finish" (rect 0 0 56 12)(font "Arial" ))
  245. (text "send_finish" (rect 99 43 155 55)(font "Arial" ))
  246. (line (pt 176 48)(pt 160 48)(line_width 1))
  247. )
  248. (parameter
  249. "div_par"
  250. "0000000101000101"
  251. ""
  252. (type "PARAMETER_UNSIGNED_BIN") )
  253. (drawing
  254. (rectangle (rect 16 16 160 240)(line_width 1))
  255. )
  256. (annotation_block (parameter)(rect 776 256 1024 288))
  257. )
  258. (symbol
  259. (rect 288 304 472 560)
  260. (text "text_f" (rect 5 0 34 12)(font "Arial" ))
  261. (text "inst4" (rect 8 240 31 252)(font "Arial" ))
  262. (port
  263. (pt 0 32)
  264. (input)
  265. (text "sysclk" (rect 0 0 33 12)(font "Arial" ))
  266. (text "sysclk" (rect 21 27 54 39)(font "Arial" ))
  267. (line (pt 0 32)(pt 16 32)(line_width 1))
  268. )
  269. (port
  270. (pt 0 48)
  271. (input)
  272. (text "inclk" (rect 0 0 22 12)(font "Arial" ))
  273. (text "inclk" (rect 21 43 43 55)(font "Arial" ))
  274. (line (pt 0 48)(pt 16 48)(line_width 1))
  275. )
  276. (port
  277. (pt 0 64)
  278. (input)
  279. (text "send_finish" (rect 0 0 56 12)(font "Arial" ))
  280. (text "send_finish" (rect 21 59 77 71)(font "Arial" ))
  281. (line (pt 0 64)(pt 16 64)(line_width 1))
  282. )
  283. (port
  284. (pt 0 80)
  285. (input)
  286. (text "key" (rect 0 0 18 12)(font "Arial" ))
  287. (text "key" (rect 21 75 39 87)(font "Arial" ))
  288. (line (pt 0 80)(pt 16 80)(line_width 1))
  289. )
  290. (port
  291. (pt 184 32)
  292. (output)
  293. (text "outclk" (rect 0 0 29 12)(font "Arial" ))
  294. (text "outclk" (rect 134 27 163 39)(font "Arial" ))
  295. (line (pt 184 32)(pt 168 32)(line_width 1))
  296. )
  297. (port
  298. (pt 184 48)
  299. (output)
  300. (text "seg_duan[7..0]" (rect 0 0 73 12)(font "Arial" ))
  301. (text "seg_duan[7..0]" (rect 90 43 163 55)(font "Arial" ))
  302. (line (pt 184 48)(pt 168 48)(line_width 3))
  303. )
  304. (port
  305. (pt 184 64)
  306. (output)
  307. (text "seg_wei[7..0]" (rect 0 0 64 12)(font "Arial" ))
  308. (text "seg_wei[7..0]" (rect 99 59 163 71)(font "Arial" ))
  309. (line (pt 184 64)(pt 168 64)(line_width 3))
  310. )
  311. (port
  312. (pt 184 80)
  313. (output)
  314. (text "start" (rect 0 0 22 12)(font "Arial" ))
  315. (text "start" (rect 141 75 163 87)(font "Arial" ))
  316. (line (pt 184 80)(pt 168 80)(line_width 1))
  317. )
  318. (port
  319. (pt 184 96)
  320. (output)
  321. (text "count1[7..0]" (rect 0 0 59 12)(font "Arial" ))
  322. (text "count1[7..0]" (rect 104 91 163 103)(font "Arial" ))
  323. (line (pt 184 96)(pt 168 96)(line_width 3))
  324. )
  325. (port
  326. (pt 184 112)
  327. (output)
  328. (text "count2[7..0]" (rect 0 0 59 12)(font "Arial" ))
  329. (text "count2[7..0]" (rect 104 107 163 119)(font "Arial" ))
  330. (line (pt 184 112)(pt 168 112)(line_width 3))
  331. )
  332. (port
  333. (pt 184 128)
  334. (output)
  335. (text "count3[7..0]" (rect 0 0 59 12)(font "Arial" ))
  336. (text "count3[7..0]" (rect 104 123 163 135)(font "Arial" ))
  337. (line (pt 184 128)(pt 168 128)(line_width 3))
  338. )
  339. (port
  340. (pt 184 144)
  341. (output)
  342. (text "count4[7..0]" (rect 0 0 59 12)(font "Arial" ))
  343. (text "count4[7..0]" (rect 104 139 163 151)(font "Arial" ))
  344. (line (pt 184 144)(pt 168 144)(line_width 3))
  345. )
  346. (port
  347. (pt 184 160)
  348. (output)
  349. (text "count5[7..0]" (rect 0 0 59 12)(font "Arial" ))
  350. (text "count5[7..0]" (rect 104 155 163 167)(font "Arial" ))
  351. (line (pt 184 160)(pt 168 160)(line_width 3))
  352. )
  353. (port
  354. (pt 184 176)
  355. (output)
  356. (text "count6[7..0]" (rect 0 0 59 12)(font "Arial" ))
  357. (text "count6[7..0]" (rect 104 171 163 183)(font "Arial" ))
  358. (line (pt 184 176)(pt 168 176)(line_width 3))
  359. )
  360. (port
  361. (pt 184 192)
  362. (output)
  363. (text "count7[7..0]" (rect 0 0 59 12)(font "Arial" ))
  364. (text "count7[7..0]" (rect 104 187 163 199)(font "Arial" ))
  365. (line (pt 184 192)(pt 168 192)(line_width 3))
  366. )
  367. (port
  368. (pt 184 208)
  369. (output)
  370. (text "count8[7..0]" (rect 0 0 59 12)(font "Arial" ))
  371. (text "count8[7..0]" (rect 104 203 163 215)(font "Arial" ))
  372. (line (pt 184 208)(pt 168 208)(line_width 3))
  373. )
  374. (drawing
  375. (rectangle (rect 16 16 168 240)(line_width 1))
  376. )
  377. )
  378. (connector
  379. (pt 288 352)
  380. (pt 248 352)
  381. )
  382. (connector
  383. (pt 576 224)
  384. (pt 280 224)
  385. )
  386. (connector
  387. (pt 280 224)
  388. (pt 280 336)
  389. )
  390. (connector
  391. (pt 288 368)
  392. (pt 264 368)
  393. )
  394. (connector
  395. (pt 264 592)
  396. (pt 264 368)
  397. )
  398. (connector
  399. (pt 264 592)
  400. (pt 856 592)
  401. )
  402. (connector
  403. (pt 240 288)
  404. (pt 560 288)
  405. )
  406. (connector
  407. (pt 472 336)
  408. (pt 496 336)
  409. )
  410. (connector
  411. (pt 496 336)
  412. (pt 496 240)
  413. )
  414. (connector
  415. (pt 472 352)
  416. (pt 512 352)
  417. (bus)
  418. )
  419. (connector
  420. (pt 536 368)
  421. (pt 536 208)
  422. (bus)
  423. )
  424. (connector
  425. (pt 576 320)
  426. (pt 648 320)
  427. )
  428. (connector
  429. (pt 576 224)
  430. (pt 576 320)
  431. )
  432. (connector
  433. (pt 856 336)
  434. (pt 824 336)
  435. )
  436. (connector
  437. (pt 856 592)
  438. (pt 856 336)
  439. )
  440. (connector
  441. (pt 560 336)
  442. (pt 648 336)
  443. )
  444. (connector
  445. (pt 560 288)
  446. (pt 560 336)
  447. )
  448. (connector
  449. (pt 824 320)
  450. (pt 872 320)
  451. )
  452. (connector
  453. (pt 472 368)
  454. (pt 536 368)
  455. (bus)
  456. )
  457. (connector
  458. (pt 624 384)
  459. (pt 472 384)
  460. )
  461. (connector
  462. (pt 624 384)
  463. (pt 624 368)
  464. )
  465. (connector
  466. (pt 648 368)
  467. (pt 624 368)
  468. )
  469. (connector
  470. (pt 472 400)
  471. (pt 648 400)
  472. (bus)
  473. )
  474. (connector
  475. (pt 472 416)
  476. (pt 648 416)
  477. (bus)
  478. )
  479. (connector
  480. (pt 472 432)
  481. (pt 648 432)
  482. (bus)
  483. )
  484. (connector
  485. (pt 472 448)
  486. (pt 648 448)
  487. (bus)
  488. )
  489. (connector
  490. (pt 648 464)
  491. (pt 472 464)
  492. (bus)
  493. )
  494. (connector
  495. (pt 472 480)
  496. (pt 648 480)
  497. (bus)
  498. )
  499. (connector
  500. (pt 648 496)
  501. (pt 472 496)
  502. (bus)
  503. )
  504. (connector
  505. (pt 472 512)
  506. (pt 648 512)
  507. (bus)
  508. )
  509. (connector
  510. (pt 512 176)
  511. (pt 512 352)
  512. (bus)
  513. )
  514. (connector
  515. (pt 288 384)
  516. (pt 200 384)
  517. )
  518. (junction (pt 280 336))

该文件为工程的顶层文件,相当于C语言中的主函数,下面的文件相当C语言中的子函数。

频率采集模块

test_f.v

  1. module text_f(
  2. sysclk,
  3. inclk,
  4. outclk,
  5. seg_duan,
  6. seg_wei,
  7. start,
  8. send_finish,
  9. key,
  10. count1,
  11. count2,
  12. count3,
  13. count4,
  14. count5,
  15. count6,
  16. count7,
  17. count8
  18. );
  19. input sysclk,inclk;//系统时钟
  20. output outclk;
  21. output start;
  22. input send_finish;
  23. input key;
  24. output [7:0] seg_duan,seg_wei;//输入时钟
  25. output [7:0] count1;
  26. output [7:0] count2;
  27. output [7:0] count3;
  28. output [7:0] count4;
  29. output [7:0] count5;
  30. output [7:0] count6;
  31. output [7:0] count7;
  32. output [7:0] count8;
  33. wire sysclk,inclk;//系统时钟//输入时钟
  34. reg start;
  35. reg [24:0] clk_counter;//时钟计数
  36. reg clk_div;//分频后的时钟
  37. reg [7:0] seg_duan,seg_wei;//输入时钟
  38. reg [7:0] data;
  39. reg [3:0] counter1,counter2,counter3,counter4,counter5,counter6,counter7,counter8;
  40. reg [7:0] count1,count2,count3,count4,count5,count6,count7,count8;
  41. reg [3:0] count1_seg,count2_seg,count3_seg,count4_seg,count5_seg,count6_seg,count7_seg,count8_seg;
  42. reg [14:0] counter1_clk;
  43. reg [15:0] counter2_clk;
  44. reg test_clk1;
  45. reg test_clk2;
  46. reg outclk;
  47. /***********************产生测试时钟1**************************************/
  48. always @(posedge sysclk)
  49. begin
  50. if(counter1_clk == 15'b110_0001_1010_1000)
  51. begin
  52. test_clk1<=~test_clk1;//500HZ
  53. counter1_clk<=15'b0;
  54. end
  55. else
  56. counter1_clk<=counter1_clk+1'b1;
  57. end
  58. /***********************产生测试时钟2**************************************/
  59. always @(posedge sysclk)
  60. begin
  61. if(counter2_clk == 16'd50000)
  62. begin
  63. test_clk2<=~test_clk2;//250HZ
  64. counter2_clk<=16'b0;
  65. end
  66. else
  67. counter2_clk<=counter2_clk+1'b1;
  68. end
  69. /***********************选择输出的时钟**************************************/
  70. always @(key)
  71. begin
  72. if(key == 1'b1)
  73. outclk <= test_clk1;
  74. else
  75. outclk <= test_clk2;
  76. end
  77. /**********************产生1HZ的时钟************************************/
  78. always @(posedge sysclk)
  79. begin
  80. if(clk_counter==25'b1_0111_1101_0111_1000_0100_0000)
  81. begin
  82. clk_div<=~clk_div;
  83. clk_counter<=25'b0;
  84. end
  85. else
  86. clk_counter<=clk_counter+1'b1;
  87. end
  88. /*********************测试待测信号***********************************/
  89. always @(posedge outclk)
  90. begin
  91. if(clk_div)
  92. begin
  93. start <= 1'b0;
  94. if(counter1==4'b1001)
  95. begin
  96. counter1<=4'b0;
  97. counter2<=counter2+1'b1;
  98. if(counter2==4'b1001)
  99. begin
  100. counter2<=4'b0;
  101. counter3<=counter3+1'b1;
  102. if(counter3==4'b1001)
  103. begin
  104. counter3<=4'b0;
  105. counter4<=counter4+1'b1;
  106. if(counter4==4'b1001)
  107. begin
  108. counter4<=4'b0;
  109. counter5<=counter5+1'b1;
  110. if(counter5==4'b1001)
  111. begin
  112. counter5<=4'b0;
  113. counter6<=counter6+1'b1;
  114. if(counter6==4'b1001)
  115. begin
  116. counter6<=4'b0;
  117. counter7<=counter7+1'b1;
  118. if(counter7==4'b1001)
  119. begin
  120. counter7<=4'b0;
  121. counter8<=counter8+1'b1;
  122. if(counter8==4'b1001)
  123. begin
  124. counter8<=4'b0;
  125. end
  126. end
  127. end
  128. end
  129. end
  130. end
  131. end
  132. end
  133. else
  134. counter1<=counter1+1'b1;
  135. end
  136. else
  137. /*******************测试结果寄存********************************/
  138. begin
  139. start <= 1'b1;
  140. if(counter1!=4'b0000|counter2!=4'b0000!=4'b0000|counter3!=4'b0000|counter4!=4'b0000|
  141. counter5!=4'b0000|counter6!=4'b0000|counter7!=4'b0000|counter8!=4'b0000)
  142. begin
  143. count1<=counter1+8'd48;
  144. count2<=counter2+8'd48;
  145. count3<=counter3+8'd48;
  146. count4<=counter4+8'd48;
  147. count5<=counter5+8'd48;
  148. count6<=counter6+8'd48;
  149. count7<=counter7+8'd48;
  150. count8<=counter8+8'd48;
  151. count1_seg<=counter1;
  152. count2_seg<=counter2;
  153. count3_seg<=counter3;
  154. count4_seg<=counter4;
  155. count5_seg<=counter5;
  156. count6_seg<=counter6;
  157. count7_seg<=counter7;
  158. count8_seg<=counter8;
  159. counter1<=4'b0000;
  160. counter2<=4'b0000;
  161. counter3<=4'b0000;
  162. counter4<=4'b0000;
  163. counter5<=4'b0000;
  164. counter6<=4'b0000;
  165. counter7<=4'b0000;
  166. counter8<=4'b0000;
  167. end
  168. end
  169. end
  170. /*****************测试结果数码管显示*************************/
  171. always @(clk_counter , count1_seg , count2_seg , count3_seg , count4_seg , count5_seg , count6_seg , count7_seg , count8_seg,data)
  172. begin
  173. case(clk_counter[15:13])//数码管位扫描
  174. 3'b000:begin seg_wei<=8'b1111_1110;data<=count1_seg;end
  175. 3'b001:begin seg_wei<=8'b1111_1101;data<=count2_seg;end
  176. 3'b010:begin seg_wei<=8'b1111_1011;data<=count3_seg;end
  177. 3'b011:begin seg_wei<=8'b1111_0111;data<=count4_seg;end
  178. default:begin seg_wei<=8'bx;data<=4'bx;end
  179. endcase
  180. case(data[3:0])//数码管显示
  181. 4'b0000:begin seg_duan<=8'b1100_0000;end//0
  182. 4'b0001:begin seg_duan<=8'b1111_1001;end//1
  183. 4'b0010:begin seg_duan<=8'b1010_0100;end//2
  184. 4'b0011:begin seg_duan<=8'b1011_0000;end//3
  185. 4'b0100:begin seg_duan<=8'b1001_1001;end//4
  186. default:seg_duan<=8'bx;
  187. endcase
  188. end
  189. endmodule

串口模块

uart.v

//串口通信__FPGA和上位机通信(波特率:9600bps,10个bit是1位起始位,8个数据位,1个结束)

  1. module uart(
  2. clk,
  3. rst,
  4. rxd,
  5. txd,
  6. start,
  7. data_cnt,
  8. count1,
  9. count2,
  10. count3,
  11. count4,
  12. count5,
  13. count6,
  14. count7,
  15. count8,
  16. send_finish
  17. );
  18. input clk; //系统50MHZ时钟
  19. input rst; //复位
  20. input rxd; //串行数据接收端
  21. output txd; //串行数据发送端
  22. input start; //开始采集信号
  23. input[3:0] data_cnt; //数据位标志
  24. output send_finish; //发送完成标志
  25. input [7:0] count1;
  26. input [7:0] count2;
  27. input [7:0] count3;
  28. input [7:0] count4;
  29. input [7:0] count5;
  30. input [7:0] count6;
  31. input [7:0] count7;
  32. input [7:0] count8;
  33. reg[15:0] div_reg; //分频计数器,分频值由波特率决定。分频后得到频率8倍波特率的时钟
  34. reg[2:0] div8_tras_reg; //该寄存器的计数值对应发送时当前位于的时隙数
  35. reg[3:0] state_tras; //发送状态寄存器
  36. reg clkbaud_tras; //以波特率为频率的发送使能信号
  37. reg clkbaud8x; //以8倍波特率为频率的时钟,它的作用是将发送或接受一个bit的时钟周期分为8个时隙
  38. reg trasstart; //开始发送标志
  39. reg send_finish;
  40. reg txd_reg; //发送寄存器
  41. reg[7:0] rxd_buf; //接受数据缓存
  42. reg[7:0] txd_buf; //发送数据缓存
  43. reg[3:0] send_state; //发送状态寄存器
  44. parameter div_par=16'h145;
  45. //分频参数,其值由对应的波特率计算而得,按此参数分频的时钟频率是波倍特率的8
  46. //倍,此处值对应9600的波特率,即分频出的时钟频率是9600*8 (CLK50M)
  47. assign txd = txd_reg;
  48. // assign send_state=data_cnt;
  49. /*******分频得到8倍波特率的时钟*********/
  50. always@(posedge clk )
  51. begin
  52. if(!rst)
  53. div_reg<=0;
  54. else begin
  55. if(div_reg==div_par-1'b1)
  56. div_reg<=0;
  57. else
  58. div_reg<=div_reg+1'b1;
  59. end
  60. end
  61. always@(posedge clk)
  62. begin
  63. if(!rst)
  64. clkbaud8x<=0;
  65. else if(div_reg==div_par-1'b1)
  66. clkbaud8x<=~clkbaud8x;//分频得到8倍波特率的时钟:clkbaud8x
  67. end
  68. // *******************************/
  69. always@(posedge clkbaud8x or negedge rst)//clkbaud8x
  70. begin
  71. if(!rst)
  72. div8_tras_reg<=0;
  73. else if(trasstart)
  74. div8_tras_reg<=div8_tras_reg+1'b1;//发送开始后,时隙数在8倍波特率的时钟下加1循环
  75. end
  76. always@(div8_tras_reg)
  77. begin
  78. if(div8_tras_reg==7)
  79. clkbaud_tras=1;//在第7个时隙,发送使能信号有效,将数据发出
  80. else
  81. clkbaud_tras=0;
  82. end
  83. // *********发送数据模块***************/
  84. always@(posedge clkbaud8x or negedge rst)//clkbaud8x
  85. begin
  86. if(!rst)
  87. begin
  88. txd_reg<=1;//发送寄存器置高
  89. trasstart<=0;//开始发送标志置低
  90. txd_buf<=8'h00;//发送缓存器清零
  91. state_tras<=0;//发送状态寄存器清零
  92. send_finish <= 1'b0;
  93. send_state<=4'd0;
  94. end
  95. else
  96. if(start == 1'b1)
  97. case(state_tras)
  98. 4'b0000: begin //发送起始位
  99. send_finish <= 1'b0;
  100. if(!trasstart&&send_state<4'd9)
  101. trasstart<=1;
  102. else if(send_state<4'd9) begin
  103. if(clkbaud_tras) begin
  104. txd_reg<=0;
  105. state_tras<=state_tras+1'b1;
  106. end
  107. end
  108. else begin
  109. state_tras<=0;
  110. end
  111. end
  112. 4'b0001: begin //发送第1位
  113. if(clkbaud_tras) begin
  114. txd_reg<=txd_buf[0];
  115. txd_buf[6:0]<=txd_buf[7:1];
  116. state_tras<=state_tras+1'b1;
  117. end
  118. end
  119. 4'b0010: begin //发送第2位
  120. if(clkbaud_tras) begin
  121. txd_reg<=txd_buf[0];
  122. txd_buf[6:0]<=txd_buf[7:1];
  123. state_tras<=state_tras+1'b1;
  124. end
  125. end
  126. 4'b0011: begin //发送第3位
  127. if(clkbaud_tras) begin
  128. txd_reg<=txd_buf[0];
  129. txd_buf[6:0]<=txd_buf[7:1];
  130. state_tras<=state_tras+1'b1;
  131. end
  132. end
  133. 4'b0100: begin //发送第4位
  134. if(clkbaud_tras) begin
  135. txd_reg<=txd_buf[0];
  136. txd_buf[6:0]<=txd_buf[7:1];
  137. state_tras<=state_tras+1'b1;
  138. end
  139. end
  140. 4'b0101: begin //发送第5位
  141. if(clkbaud_tras) begin
  142. txd_reg<=txd_buf[0];
  143. txd_buf[6:0]<=txd_buf[7:1];
  144. state_tras<=state_tras+1'b1;
  145. end
  146. end
  147. 4'b0110: begin //发送第6位
  148. if(clkbaud_tras) begin
  149. txd_reg<=txd_buf[0];
  150. txd_buf[6:0]<=txd_buf[7:1];
  151. state_tras<=state_tras+1'b1;
  152. end
  153. end
  154. 4'b0111: begin //发送第7位
  155. if(clkbaud_tras) begin
  156. txd_reg<=txd_buf[0];
  157. txd_buf[6:0]<=txd_buf[7:1];
  158. state_tras<=state_tras+1'b1;
  159. end
  160. end
  161. 4'b1000: begin //发送第8位
  162. if(clkbaud_tras) begin
  163. txd_reg<=txd_buf[0];
  164. txd_buf[6:0]<=txd_buf[7:1];
  165. state_tras<=state_tras+1'b1;
  166. end
  167. end
  168. 4'b1001: begin //发送停止位
  169. if(clkbaud_tras) begin
  170. txd_reg<=1;
  171. txd_buf<=8'h00;
  172. state_tras<=state_tras+1'b1;
  173. end
  174. end
  175. 4'b1111:begin
  176. if(clkbaud_tras) begin
  177. state_tras<=state_tras+1'b1;
  178. send_state<=send_state+1'b1;
  179. trasstart<=0;
  180. case(send_state)
  181. 4'b0000:
  182. txd_buf<=count8;//
  183. 4'b0001:
  184. txd_buf<=count7;//
  185. 4'b0010:
  186. txd_buf<=count6;//
  187. 4'b0011:
  188. txd_buf<=count5;//
  189. 4'b1000:
  190. begin
  191. txd_buf<=8'd10;//换行符
  192. send_finish <= 1'b1;
  193. send_state<=4'b0000;
  194. end
  195. default:
  196. txd_buf<=8'd0;
  197. endcase
  198. end
  199. end
  200. default: begin
  201. if(clkbaud_tras) begin
  202. state_tras<=state_tras+1'b1;
  203. trasstart<=1;
  204. end
  205. end
  206. endcase
  207. end
  208. endmodule

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/467726
推荐阅读
相关标签
  

闽ICP备14008679号