首页 > 代码库 > 00h

00h

C语言中的宏定义

  1. //程序设计:傅越千
  2. //2009.7.30
  3. #include <c8051f360.h>
  4. #include <stdio.h>
  5. #include <absacc.h>
  6. #define WDATADDR XBYTE[0XC009] //LCD写数据地址
  7. #define RDATADDR XBYTE[0XC00B] //LCD读数据地址
  8. #define WCOMADDR XBYTE[0XC008] //LCD写命令地址
  9. #define RCOMADDR XBYTE[0XC00A] //LCD读命令地址
  10. #define KEYCS XBYTE[0XC00C] //键盘片选地址
  11. sbit LCD_RST=P3^0;
  12. sbit START=P3^2;
  13. unsigned char code CHINESE1[]={"高速数据采集系统"};
  14. unsigned char code SINTAB[]={\
  15. 0x7f,0x82,0x85,0x88,0x8b,0x8f,0x92,0x95,0x98,0x9b,0x9e,0xa1,0xa4,0xa7,0xaa,0xad,\
  16. 0xb0,0xb3,0xb6,0xb8,0xbb,0xbe,0xc1,0xc3,0xc6,0xc8,0xcb,0xcd,0xd0,0xd2,0xd5,0xd7,\
  17. 0xd9,0xdb,0xdd,0xe0,0xe2,0xe4,0xe5,0xe7,0xe9,0xeb,0xec,0xee,0xef,0xf1,0xf2,0xf4,\
  18. 0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfb,0xfc,0xfd,0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,\
  19. 0xff,0xfe,0xfe,0xfe,0xfe,0xfe,0xfd,0xfd,0xfc,0xfb,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,\
  20. 0xf5,0xf4,0xf2,0xf1,0xef,0xee,0xec,0xeb,0xe9,0xe7,0xe5,0xe4,0xe2,0xe0,0xdd,0xdb,\
  21. 0xd9,0xd7,0xd5,0xd2,0xd0,0xcd,0xcb,0xc8,0xc6,0xc3,0xc1,0xbe,0xbb,0xb8,0xb6,0xb3,\
  22. 0xb0,0xad,0xaa,0xa7,0xa4,0xa1,0x9e,0x9b,0x98,0x95,0x92,0x8f,0x8b,0x88,0x85,0x82,\
  23. 0x7f,0x7c,0x79,0x76,0x73,0x6f,0x6c,0x69,0x66,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,\
  24. 0x4e,0x4b,0x48,0x46,0x43,0x40,0x3d,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x29,0x27,\
  25. 0x25,0x23,0x21,0x1e,0x1c,0x1a,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,\
  26. 0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,\
  27. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,\
  28. 0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1a,0x1c,0x1e,0x21,0x23,\
  29. 0x25,0x27,0x29,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3d,0x40,0x43,0x46,0x48,0x4b,\
  30. 0x4e,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x73,0x76,0x79,0x7c} ;
  31. unsigned char key_num=0xff; //存键号
  32. unsigned char idata disp[128];
  33. unsigned char flag=0;
  34. void OSC_INIT (void)
  35. {
  36. SFRPAGE=0X0F;
  37. OSCICL=OSCICL+4;
  38. OSCICN=0XC2;
  39. CLKSEL=0X30;
  40. SFRPAGE=0;
  41. }
  42. //********************************************
  43. void IO_INIT(void)
  44. {
  45. SFRPAGE=0X0F;
  46. P0MDIN=0Xe7;
  47. P0MDOUT=0X83;
  48. P0SKIP=0XF9;
  49. P1MDIN=0XFF;
  50. P1MDOUT=0XFF;
  51. P1SKIP=0XFF;
  52. P2MDIN=0XFE;
  53. P2MDOUT=0XFF;
  54. P2SKIP=0XFF;
  55. P3MDIN=0XFF;
  56. P3MDOUT=0XFB; //FF*********
  57. P3SKIP=0XFD;
  58. P4MDOUT=0XFF;
  59. XBR0=0X09;
  60. XBR1=0X40; //禁止弱上拉,允许交叉开关 *****
  61. SFRPAGE=0X0;
  62. }
  63. //********************************************
  64. void XRAM_INIT(void)
  65. {
  66. SFRPAGE=0X0F;
  67. EMI0CF=0X07;
  68. SFRPAGE=0;
  69. }
  70. //********************************************
  71. void SMB_INIT(void)
  72. {
  73. SMB0CF=0XC1;
  74. }
  75. //********************************************
  76. void UART_INIT(void)
  77. {
  78. SCON0=0X0;
  79. }
  80. //********************************************
  81. void DAC_INIT(void)
  82. {
  83. IDA0CN=0XF2;
  84. }
  85. //********************************************
  86. void ADC_INIT(void)
  87. {
  88. REF0CN=0; //VDD为基准
  89. AMX0P=0X08; //正端接P20
  90. AMX0N=0X1F; //负端接GND
  91. ADC0CF=0X2C; //左对齐,转换时钟2MHZ
  92. ADC0CN=0X80; //写ADOBUSY启动AD
  93. }
  94. //********************************************
  95. void INT0_INIT(void)
  96. {
  97. IT01CF=0X65; //选择P0.6为INT1,P0.5为INT0
  98. IT0=1; //下降沿触发
  99. IT1=1;
  100. }
  101. //********************************************
  102. void TIMER_INIT(void)
  103. {
  104. TMOD=0x11; //T0、T1方式1
  105. CKCON=0; //系统时钟12分频
  106. TL0=0X78;
  107. TH0=0XEC; //10MS
  108. TL1=0X0C;
  109. TH1=0XFE; //0.5MS
  110. TMR2CN=0X04; //16位自动重装
  111. TMR2RLL=0XF0; //10MS
  112. TMR2RLH=0XD8;
  113. TMR3CN=0X0C; //双8位自动重装入,系统时钟1/12
  114. TMR3RLL=0XE0; //定时100us
  115. TMR3RLH=0XFF;
  116. TR0=1;
  117. TR1=1;
  118. }
  119. //***********************************************
  120. void PCA_INIT(void)
  121. {
  122. PCA0CN=0X40; //允许PCA计数器、定时器
  123. PCA0MD=0; //禁止看门狗定时器
  124. }
  125. //***********************************************
  126. void INT_INIT(void)
  127. {
  128. EX0=1; //INIT0,键盘
  129. PX0=0; //INT0为低优先级
  130. ET0=0; //T0
  131. ET1=0; //T1
  132. ET2=0; //T2
  133. EIE1=0X0; //0X08,允许ADC中断
  134. ES0=0; //uart
  135. EA=1;
  136. }
  137. //**************************************************
  138. void Init_device(void)
  139. {
  140. OSC_INIT();
  141. IO_INIT();
  142. XRAM_INIT();
  143. SMB_INIT();
  144. UART_INIT();
  145. // DAC_INIT();
  146. // ADC_INIT();
  147. TIMER_INIT();
  148. PCA_INIT();
  149. INT0_INIT();
  150. INT_INIT();
  151. }
  152. //**************************************************
  153. //--------------------------------------------------------
  154. void LCD_REST(void)
  155. {
  156. int i;
  157. LCD_RST=0;
  158. for(i=0;i<255;i++);
  159. LCD_RST=1;
  160. }
  161. //********************************************************
  162. void LCD_WC(unsigned char command) //LCD写命令
  163. {
  164. while(RCOMADDR&0X80);
  165. WCOMADDR=command;
  166. }
  167. //********************************************************
  168. void LCD_INIT(void) //LCD初始化
  169. {
  170. LCD_WC(0X30); //设为基本命令集
  171. LCD_WC(0X01);
  172. LCD_WC(0X02); //将DDRAM填满20H,并设定DDRAM地址计数器为0
  173. LCD_WC(0X0C); //开整体显示
  174. }
  175. //***********************************************************
  176. void LCD_WD(unsigned char d) //LCD写数据
  177. {
  178. while(RCOMADDR&0X80);
  179. WDATADDR=d;
  180. }
  181. //**********************************************************
  182. void LCD_HZ(unsigned char x,unsigned char temp[]) //显示一行字符
  183. {
  184. int i=0;
  185. LCD_WC(x); //x代表位置,=0x80对应左上角
  186. while(temp[i]!=0)
  187. {
  188. LCD_WD(temp[i]);
  189. i++;
  190. }
  191. }
  192. //********************************************************
  193. void LCD_BYTE(unsigned char x,unsigned char temp) //显示一行字符
  194. {
  195. LCD_WC(x); //x代表位置,=0x80对应左上角
  196. LCD_WD(temp);
  197. }
  198. //*********************************************************
  199. void LCD_CLR(void) //LCD清屏
  200. {
  201. LCD_WC(0X01);
  202. }
  203. //*********************************************************
  204. void INIT_XSRAM(void) //ROM中的SIN模拟数据复制到disp
  205. {
  206. int i,j;
  207. for(i=0,j=0 ;i<128;i++){
  208. disp[i]=SINTAB[j];
  209. j=j+2;
  210. }
  211. }
  212. //********************************************************
  213. void CLRGDRAM(void) //清LCD VRAM
  214. {
  215. int i,j;
  216. for(i=0;i<32;i++){
  217. LCD_WC((i|0x80));
  218. LCD_WC(0X80);
  219. for(j=0;j<16;j++) LCD_WD(0x0);
  220. }
  221. for(i=0;i<32;i++){
  222. LCD_WC((i|0x80));
  223. LCD_WC(0X88);
  224. for(j=0;j<16;j++) LCD_WD(0x00);
  225. }
  226. }
  227. //********************************************************
  228. void WR_GDRAM(void) //将disp[]数据送LCD显示
  229. {
  230. unsigned char buf[16];
  231. unsigned char code tab[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
  232. int i,j,k;
  233. for(k=0;k<0x40;k++){
  234. for(i=0;i<16;i++) buf[i]=0;
  235. for(i=0,j=0;i<128;i++){
  236. if(((~disp[i])>>2)==k){
  237. buf[i/8]=buf[i/8]|tab[i%8];
  238. }
  239. }
  240. if(k<32) {LCD_WC(k|0x80);LCD_WC(0X80);}
  241. else {LCD_WC((k-0x20)|0x80);LCD_WC(0X88);}
  242. for(i=0;i<16;i++) LCD_WD(buf[i]);
  243. }
  244. }
  245. //********************************************************
  246. void KEY_INIT0(void) interrupt 0
  247. {
  248. key_num=KEYCS&0x0f;
  249. }
  250. //********************************************************
  251. void GET_DATA(void) interrupt 2 //取FPGA的采样数据
  252. {
  253. unsigned char xdata *p;
  254. int i;
  255. p=0x4000; //双口RAM的首地址
  256. for(i=0;i<128;i++,p++) disp[i]=*p;
  257. flag=1; //设置标志
  258. }
  259. main()
  260. {
  261. int i;
  262. Init_device();
  263. LCD_REST();
  264. LCD_INIT();
  265. LCD_HZ(0x90,CHINESE1);
  266. LCD_INIT();
  267. LCD_WC(0X34);
  268. CLRGDRAM(); //清LCD
  269. INIT_XSRAM();
  270. WR_GDRAM(); //显示正弦
  271. LCD_WC(0X36);
  272. LCD_WC(0X30);
  273. IE1=0;
  274. EX1=1;
  275. while(1)
  276. {
  277. if(flag) {
  278. flag=0;
  279. LCD_INIT();
  280. LCD_WC(0X34);
  281. CLRGDRAM(); //清LCD
  282. WR_GDRAM(); //显示正弦
  283. LCD_WC(0X36);
  284. LCD_WC(0X30);
  285. }
  286. if((key_num&0xf0)==0)
  287. {
  288. switch(key_num)
  289. {
  290. case 0:
  291. START=0;
  292. for(i=0;i<20000;i++);
  293. START=1;
  294. break;
  295. case 1: break;
  296. }
  297. key_num=0xff;
  298. }
  299. }
  300. }

技术分享
技术分享



来自为知笔记(Wiz)


附件列表

  • 11.png

 

00h