微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > Freescale 9S12 系列单片机应用笔记(SCI)1

Freescale 9S12 系列单片机应用笔记(SCI)1

时间:11-20 来源:互联网 点击:

SCI模块应用笔记(1)

UART,也就是异步串行通讯接口是单片机中最常见的外设,几乎每种类型的单片机都必备1到2个UART接口,9S12系列单片机也不例外。不过,摩托罗拉给他自己的单片机的串口起了个很有个性的缩写名称SCI(serialcommunicationinterface),其实就是我们常说的UART。

各种单片机串口的编程都大同小异,无非就是设置波特率、起始位、停止位、校验位等等。下面通过给出编程例子的方式分别介绍。

设置波特率

  1. /**
  2. *SettheBaudRateoftheSCI.
  3. *@paramport,portcanbeSCI0/SCI1
  4. *@parambaudRate,thewantedbaudrate.
  5. *@parambusClk,TheSCImoduleclock.
  6. */
  7. voidSCISetBaudRate(unsignedcharport,unsignedlongbaudRate,unsignedlongbusClk)
  8. {
  9. unsignedshortbaudRateReg;
  10. baudRateReg=(unsignedshort)(busClk/baudRate/16);
  11. if(port==SCI0)
  12. {
  13. //HerewemustwriteBDHfirst!
  14. SCI0BDH=(0x1f&(baudRateReg>>8));
  15. SCI0BDL=(0xff&baudRateReg);
  16. }
  17. elseif(port==SCI1)
  18. {
  19. SCI1BDH=(0x1f&(baudRateReg>>8));
  20. SCI1BDL=(0xff&baudRateReg);
  21. }
  22. else
  23. {
  24. //Somethingmustgowrong.Donothinghere!
  25. }
  26. }

设置奇偶校验位

  1. /**
  2. *Enable/DisableparityfunctionandsettheParitytype
  3. *@paramportportcanbeSCI0/SCI1
  4. *@paramisEnable0fordisableparityfunction,1forenable
  5. *@paramtype0forEvenParity,1forOddParity
  6. */
  7. voidSCISetParity(unsignedcharport,unsignedcharisEnable,unsignedchartype)
  8. {
  9. if(port==SCI0)
  10. {
  11. SCI0CR1_PE=(isEnable&0x01);
  12. SCI0CR1_PT=(type&0x01);
  13. }
  14. elseif(port==SCI1)
  15. {
  16. SCI1CR1_PE=(isEnable&0x01);
  17. SCI1CR1_PT=(type&0x01);
  18. }
  19. else
  20. {
  21. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  22. }
  23. }


使能数据位的长度

  1. /**
  2. *SettheDataFormatModeBit
  3. *@paramportportcanbeSCI0/SCI1
  4. *@parambitsmustbe8or9
  5. */
  6. voidSCISetDataBit(unsignedcharport,unsignedcharbits)
  7. {
  8. if(port==SCI0)
  9. {
  10. switch(bits)
  11. {
  12. case8:
  13. SCI0CR1_M=0;/*1startbit,8databits,1stopbit*/
  14. break;
  15. case9:
  16. SCI0CR1_M=1;/*1startbit,9databits,1stopbit*/
  17. break;
  18. default:
  19. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  20. break;
  21. }
  22. }
  23. elseif(port==SCI1)
  24. {
  25. switch(bits)
  26. {
  27. case8:
  28. SCI1CR1_M=0;/*1startbit,8databits,1stopbit*/
  29. break;
  30. case9:
  31. SCI1CR1_M=1;/*1startbit,9databits,1stopbit*/
  32. break;
  33. default:
  34. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  35. break;
  36. }
  37. }
  38. else
  39. {
  40. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  41. }
  42. }


设置串口的工作模式

NORMAL_MODE是我们通常用的模式

LOOP_MODE是自发自收模式

SING_WIRE_MODE就是收发公用一条数据线的模式

  1. /**
  2. *Settheworkmodeofoperation
  3. *@paramportportcanbeSCI0/SCI1
  4. *@parammodemodecanbeNORMAL_MODE/LOOP_MODE/SING_WIRE_MODE
  5. */
  6. voidSCISetWorkMode(unsignedcharport,unsignedcharmode)
  7. {
  8. if(port==SCI0)
  9. {
  10. switch(mode)
  11. {
  12. caseNORMAL_MODE:
  13. SCI0CR1_LOOPS=0;
  14. SCI0CR1_RSRC=0;
  15. break;
  16. caseLOOP_MODE:
  17. SCI0CR1_LOOPS=1;
  18. SCI0CR1_RSRC=0;
  19. break;
  20. caseSING_WIRE_MODE:
  21. SCI0CR1_LOOPS=1;
  22. SCI0CR1_RSRC=1;
  23. break;
  24. default:
  25. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  26. break;
  27. }
  28. }
  29. elseif(port==SCI1)
  30. {
  31. switch(mode)
  32. {
  33. caseNORMAL_MODE:
  34. SCI1CR1_LOOPS=0;
  35. SCI1CR1_RSRC=0;
  36. break;
  37. caseLOOP_MODE:
  38. SCI1CR1_LOOPS=1;
  39. SCI1CR1_RSRC=0;
  40. break;
  41. caseSING_WIRE_MODE:
  42. SCI1CR1_LOOPS=1;
  43. SCI1CR1_RSRC=1;
  44. break;
  45. default:
  46. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  47. break;
  48. }
  49. }
  50. else
  51. {
  52. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  53. }
  54. }

设置SCI模块是否在Wait模式下工作

  1. /**
  2. *Enable/DisabletheSCIinwaitmode
  3. *@paramportportcanbeSCI0/SCI1
  4. *@parammodemodecanbeRUN_MODE/WAIT_MODE
  5. */
  6. voidSCISetPowerMode(unsignedcharport,unsignedcharmode)
  7. {
  8. if(port==SCI0)
  9. {
  10. switch(mode)
  11. {
  12. caseRUN_MODE:
  13. SCI0CR1_SCISWAI=0;
  14. break;
  15. caseWAIT_MODE:
  16. SCI0CR1_SCISWAI=1;
  17. break;
  18. default:
  19. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  20. break;
  21. }
  22. }
  23. elseif(port==SCI1)
  24. {
  25. switch(mode)
  26. {
  27. caseRUN_MODE:
  28. SCI1CR1_SCISWAI=0;
  29. break;
  30. caseWAIT_MODE:
  31. SCI1CR1_SCISWAI=1;
  32. break;
  33. default:
  34. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  35. break;
  36. }
  37. }
  38. else
  39. {
  40. //Ifcoderunhere,somethingmustgowrong.Donothinghere!
  41. }
  42. }

使能SCI模块的接收功能

  1. /**
  2. *Enable/DisableSCIReceiver
  3. *@paramportportcanbeSCI0/SCI1
  4. *@paramisEnable0disable1enable
  5. */
  6. voidSCIEnableRecv(unsignedcharport,unsignedcharisEnable)
  7. {
  8. if(port==SCI0)
  9. {
  10. SC

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top