home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / 68hc05.asc < prev    next >
Text File  |  1991-07-23  |  12KB  |  304 lines

  1. _C PROGRAMMING FOR THE 68HC05 MICROCONTROLLER_
  2. by Truman T. Van Sickle
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. #pragma portrw PORTA @ 0x00;
  8. #pragma portrw PORTA @ 0x01;
  9. #pragma portrw DDRA  @ 0x04;
  10. #pragma portrw DDRB  @ 0x05;
  11. #pragma portrw TCST   @ 0x08;
  12. #pragma portr  TCNT  @ 0x09;
  13.  
  14. #pragma memory RAMPAGE0 [64] @ 0xc0;
  15. #pragma memory ROMPROG [1024] @ 0x300;
  16.  
  17. #pragma has STOP ;
  18. #pragma has WAIT ;
  19. #pragma has MUL ;
  20.  
  21. #pragma vector __TIMER @ 0x07f8;
  22. è#pragma vector __IRQ @ 0x07fa;
  23. #pragma vector __SWI @ 0x07fc ;
  24. #pragma vector __RESET @ 0x07fe;
  25.  
  26.  
  27.  
  28.  
  29.  
  30. #pragma option v
  31. #include "hc05j1.h"
  32. #include "general.h"
  33.  
  34. /* define the global variables  */
  35. int hrs,mts,sec;    
  36. int count;
  37. int corr1,corr2,corr3;       /* used to correct the time errors  */
  38.  
  39. main(void)
  40. {
  41.     corr1=corr2=corr3=0;     /* time corrections */
  42.     TCST.RT0=0;   /* 57.3 ms cop timer */
  43.     TCST.RT1=0;   /* 8.192 ms RTI */
  44.     TCST.RTIE=1;  /* Turn on the RTI */
  45.     TCST.RTIF=0;  /* Reset interruput */
  46.     TCST.TOF=0;   /* flags */
  47.     CLI();                   /* turn on interrupt */
  48.     FOREVER
  49.     {
  50.         if(sec==60)  /* do clock things each minute  */
  51.         {
  52.             sec=0;
  53.             if(++mts==60)
  54.             {
  55.                 mts=0;
  56.                 if(++hrs==13)
  57.                     hrs=1;
  58.             }
  59.         }
  60.         WAIT();   /* wait here to save the energy  */
  61.     }
  62. }
  63. void __TIMER(void)   /* routine executed every RTI (8.192 ms) */
  64. {
  65.     TCST.TOF=0;  /* reset the interrupt */
  66.     TCST.RTIF=0; /* flags  */
  67.     if (++count==122)
  68.     {
  69.        sec++;               /* increment seconds */
  70.        if(++corr1==14)      /* To correct for 8.192 ms per tick */
  71.        {    
  72.            corr1=0;         /* run 122 ticks per second for 13  */
  73.            if(++corr2==80)  /* seconds, and 123 for the 14th second */
  74.            {                /* With this algorithm there are 14.000128 */
  75.                    corr2=0; /* actual seconds per 14 indicated. Then run */
  76.                    if(++corr3==4)
  77.                 {       
  78.                    count=1;
  79.                    corr3==0;
  80.                 } 
  81.                 else
  82.                     count=0;/* 79 of these cycles followed by 1 cycle of */
  83.             }               /* 14 seconds with 122 ticks per second. The */
  84.             else            /* elapsed time for this cycle = 1120.002048 */
  85.                count=(-1);  /* seconds for and indicated time of 1120 */
  86.        }                        /* seconds. Repeat this cycle 4 times; on */
  87.        else                     /* last cycle drop 1 tick makes indicated & 
  88.            count=0;             /* elapsed time exactly 4480 seconds.*/
  89.    }
  90. }
  91.  
  92.  
  93.  
  94.  
  95. [LISTING THREE]
  96.  
  97.                                   #pragma option v
  98.                                   #include "hc05j1.h"
  99. 0000                              #pragma portrw PORTA @ 0x00;
  100. 0001                              #pragma portrw PORTB @ 0x01;
  101. 0003                              #pragma portr  PORTD @ 0x03;
  102. 0004                              #pragma portrw DDRA  @ 0x04;
  103. 0005                              #pragma portrw DDRB  @ 0x05;
  104. 0008                              #pragma portrw TCST  @ 0x08;
  105. 0009                              #pragma portrw TCNT  @ 0x09;
  106. 07F0                              #pragma portrw __COPSVS @ 0x7f0;
  107. 07F8                              #pragma vector __TIMER @ 0x07f8;
  108. 07FA                              #pragma vector __IRQ @ 0x07fa;
  109. 07FC                              #pragma vector __SWI @ 0x07fc ;
  110. 07FE                              #pragma vector __RESET @ 0x07fe;
  111.                                   #pragma has STOP ;
  112.                                   #pragma has WAIT ;
  113.                                   #pragma has MUL ;
  114. 00C0 0040                         #pragma memory RAMPAGE0 [64] @ 0xc0;
  115. 0300 0400                         #pragma memory ROMPROG [1024] @ 0x300;
  116. 0000                              #define RT0         0  /* timer_cont_stat */
  117. 0001                              #define RT1         1
  118. 0004                              #define RTIE        4
  119. 0005                              #define TOFE        5
  120. 0006                              #define RTIF        6
  121. 0007                              #define TOF         7
  122.                                   #include "general.h"
  123. 0001                              #define TRUE        1
  124. 0000                              #define FALSE       0
  125. 0001                              #define FOREVER     while(TRUE)
  126. 0002                              #define max(a,b)    (a) > (b) ? (a) : (b)
  127. 0003                              #define min(a,b)    (a) < (b) ? (a) : (b)
  128. 0004                              #define abs(a)      (a) >= 0 ? (a) : -(a)
  129.                                   /* define the global variables  */
  130. 00C0 00C1 00C2                    int hrs,mts,sec;  
  131. 00C3                              int count;
  132. 00C4 00C5 00C6                    int corr1,corr2,corr3; /* time corrections */
  133.                                   main(void)
  134.                                   {
  135. 0300 3F C6     CLR    $C6           corr1=corr2=corr3=0; /* time corrections */
  136. 0302 3F C5     CLR    $C5         
  137. 0304 3F C4     CLR    $C4         
  138. 0306 11 08     BCLR  0,$08            TCST.RT0=0;   /* 57.3 ms cop timer */
  139. 0308 13 08     BCLR  1,$08            TCST.RT1=0;   /* 8.192 ms RTI */
  140. 030A 18 08     BSET  4,$08            TCST.RTIE=1;  /* Turn on the RTI */
  141. 030C 1D 08     BCLR  6,$08            TCST.RTIF=0;  /* Reset interruput */
  142. 030E 1F 08     BCLR  7,$08            TCST.TOF=0;   /* flags */
  143. 0310 9A        CLI                    CLI();        /* turn on interrupt */
  144.                                       FOREVER
  145.                                       {
  146. 0311 B6 C2     LDA    $C2           if(sec==60)  /* do clock things */
  147. 0313 A1 3C     CMP    #$3C        
  148. 0315 26 18     BNE    $032F       
  149. 0317 3F C2     CLR    $C2                   sec=0;
  150. 0319 3C C1     INC    $C1                   if(++mts==60)
  151. 031B B6 C1     LDA    $C1         
  152. 031D A1 3C     CMP    #$3C        
  153. 031F 26 0E     BNE    $032F       
  154.                                             {
  155. 0321 3F C1     CLR    $C1                       mts=0;
  156. 0323 3C C0     INC    $C0                       if(++hrs==13)
  157. 0325 B6 C0     LDA    $C0         
  158. 0327 A1 0D     CMP    #$0D        
  159. 0329 26 04     BNE    $032F       
  160. 032B A6 01     LDA    #$01                          hrs=1;
  161. 032D B7 C0     STA    $C0         
  162.                                             }
  163.                                         }
  164. 032F 8F        WAIT                   WAIT();   /* wait here to save energy  */
  165. 0330 20 DF     BRA    $0311         }
  166. 0332 81        RTS                }
  167.                                   void __TIMER(void)   
  168. 07F8 03 33                        {
  169. 0333 1F 08     BCLR  7,$08          TCST.TOF=0;  /* reset the interrupt */
  170. 0335 1D 08     BCLR  6,$08          TCST.RTIF=0; /* flags  */
  171. 0337 3C C3     INC    $C3             if (++count==122)
  172. 0339 B6 C3     LDA    $C3         
  173. 033B A1 7A     CMP    #$7A        
  174. 033D 26 39     BNE    $0378       
  175.                                       {
  176. 033F 3C C2     INC    $C2                sec++;  /* increment seconds */
  177. 0341 3C C4     INC    $C4                if(++corr1==14)  
  178. 0343 B6 C4     LDA    $C4         
  179. 0345 A1 0E     CMP    #$0E        
  180. 0347 26 2D     BNE    $0376       
  181.                                          {  
  182. 0349 3F C4     CLR    $C4                  corr1=0; 
  183. 034B 3C C5     INC    $C5                    if(++corr2==80)  
  184. 034D B6 C5     LDA    $C5         
  185. 034F A1 50     CMP    #$50        
  186. 0351 26 1D     BNE    $0370       
  187.                                              {     
  188. 0353 3F C5     CLR    $C5                      corr2=0;    
  189. 0355 3C C6     INC    $C6                      if(++corr3==4)
  190. 0357 B6 C6     LDA    $C6         
  191. 0359 A1 04     CMP    #$04        
  192. 035B 26 0F     BNE    $036C       
  193.                                                     {       
  194. 035D A6 01     LDA    #$01                             count=1;
  195. 035F B7 C3     STA    $C3         
  196. 0361 B6 C6     LDA    $C6                              corr3==0;
  197. 0363 26 04     BNE    $0369       
  198. 0365 A6 01     LDA    #$01        
  199. 0367 20 01     BRA    $036A       
  200. 0369 4F        CLRA               
  201.                                                     } 
  202. 036A 20 02     BRA    $036E                         else
  203. 036C 3F C3     CLR    $C3                                count=0;
  204.                                                 }              
  205. 036E 20 04     BRA    $0374                     else           
  206. 0370 A6 FF     LDA    #$FF                         count=(-1);  
  207. 0372 B7 C3     STA    $C3         
  208.                                          }                   
  209. 0374 20 02     BRA    $0378              else                
  210. 0376 3F C3     CLR    $C3                    count=0;        
  211.                                      }
  212. 0378 80        RTI                }
  213. 07FE 03 00                        
  214.  
  215. SYMBOL TABLE
  216. LABEL        VALUE  LABEL        VALUE  LABEL        VALUE  LABEL        VALUE
  217.  
  218. DDRA         0004 | DDRB         0005 | FALSE        0000 | PORTA        0000 
  219. PORTB        0001 | PORTD        0003 | RT0          0000 | RT1          0001 
  220. RTIE         0004 | RTIF         0006 | TCNT         0009 | TCST         0008 
  221. TOF          0007 | TOFE         0005 | TRUE         0001 | __COPSVS     07F0 
  222. __IRQ        07FA | __RESET      07FE | __STARTUP    0000 | __SWI        07FC 
  223. __TIMER      0333 | corr1        00C4 | corr2        00C5 | corr3        00C6 
  224. count        00C3 | hrs          00C0 | main         0300 | mts          00C1 
  225. sec          00C2 | 
  226.  
  227. MEMORY USAGE MAP ('X' = Used, '-' = Unused)
  228. 0300 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
  229. 0340 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXX-------
  230. 0380 : ---------------- ---------------- ---------------- ----------------
  231. 03C0 : ---------------- ---------------- ---------------- ----------------
  232. 0700 : ---------------- ---------------- ---------------- ----------------
  233. 0740 : ---------------- ---------------- ---------------- ----------------
  234. 0780 : ---------------- ---------------- ---------------- ----------------
  235. 07C0 : ---------------- ---------------- ---------------- --------XX----XX
  236.  
  237. All other memory blocks unused.
  238.  
  239. Errors             :    0
  240. Warnings           :    0
  241.  
  242.  
  243.  
  244.  
  245.  
  246. [LISTING FOUR
  247.  
  248. #include "hc05c8.h"
  249. #include "general.h"
  250.  
  251. int hrs, mts, sec;       /* global variables   */
  252. long count=1000;
  253.  
  254. struct bothbytes         /* 16 bit int structure  */
  255. {
  256.       int hi;
  257.       int lo;
  258. };
  259. union isboth              /* and union  */
  260. {
  261.       long  l;
  262.       struct bothbytes b;
  263. };
  264. union isboth time_comp_count;
  265. registera ac;
  266. void main(void)
  267. {
  268.     int key;
  269.     TCR.OCIE = 1; /* enable output compare interrupt */
  270.     CLI();   /* enable all interrupts  */
  271.     FOREVER
  272.     {
  273.         if(sec==60)  /* do clock things each minute  */
  274.         {
  275.             sec=0;
  276.             if(++mts==60)
  277.             {
  278.                 mts=0;
  279.                 if(++hrs==13)
  280.                     hrs=1;
  281.             }
  282.         }
  283.         WAIT();   /* wait here to save the energy  */
  284.     }
  285. }
  286. void __TIMER(void)   /* time interrupt service routine  */
  287. {
  288.      /* the program gets here every millisecond  */
  289.      time_comp_count.b.hi = TCHI;
  290.      ac =TSR;           /* Clear the tof bit  */
  291.      time_comp_count.b.lo = TCLO;
  292.      time_comp_count.l += 500;  /* 500 counts per millisecond */
  293.      OCHI = time_comp_count.b.hi;
  294.      ac = TSR;           /* Clear ocf bit    */
  295.      OCLO = time_comp_count.b.lo;
  296.     if(--count)
  297.          return ;
  298.     else
  299.     {
  300.          sec++;    /* here every second  */
  301.          count=1000;/* reset count to 1 second */
  302.     }
  303. }
  304.