⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f32x_adc0_externalinput.lst

📁 silicon lab 单片机 C8051F320的ADC控制程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 184          {
 185   1         OSCICN = 0x83;                      // configure internal oscillator for
 186   1                                             // 12MHz / 1
 187   1         RSTSRC = 0x04;                      // enable missing clock detector
 188   1      }
 189          
 190          //-----------------------------------------------------------------------------
 191          // PORT_Init
 192          //-----------------------------------------------------------------------------
 193          //
 194          // Return Value:  None
 195          // Parameters:    None
 196          //
 197          // Configure the Crossbar and GPIO ports.
 198          // P0.4 - UART TX (push-pull)
 199          // P0.5 - UART RX 
 200          // P2.4 - ADC0 analog input
 201          // P2.2 - LED (push-pull)
 202          // 
 203          //-----------------------------------------------------------------------------
 204          
 205          void PORT_Init (void)
 206          {
 207   1         XBR0     = 0x01;                    // Enable UART0
 208   1         XBR1     = 0x40;                    // Enable crossbar and weak pull-ups
 209   1         P0MDOUT |= 0x10;                    // Set TX pin to push-pull
 210   1         P2MDOUT |= 0x04;                    // enable LED as a push-pull output
 211   1         P2MDIN &= ~0x10;                    // set P2.2 as an analog input
 212   1      }
 213          
 214          //-----------------------------------------------------------------------------
 215          // Timer2_Init
 216          //-----------------------------------------------------------------------------
 217          //
 218          // Return Value:  None
 219          // Parameters:    None
 220          //
 221          // Configure Timer2 to 16-bit auto-reload and generate an interrupt at 100uS 
 222          // intervals.  Timer 2 overflow automatically triggers ADC0 conversion.
 223          // 
 224          //-----------------------------------------------------------------------------
 225          
 226          void Timer2_Init (void)
 227          {
 228   1         TMR2CN  = 0x00;                     // Stop Timer2; Clear TF2;
 229   1                                             // use SYSCLK as timebase, 16-bit 
 230   1                                             // auto-reload
 231   1         CKCON  |= 0x10;                     // select SYSCLK for timer 2 source
 232   1         TMR2RL  = 65535 - (SYSCLK / 10000); // init reload value for 10uS
 233   1         TMR2    = 0xffff;                   // set to reload immediately
 234   1         TR2     = 1;                        // start Timer2
 235   1      }
 236          
 237          //-----------------------------------------------------------------------------
 238          // ADC0_Init
 239          //-----------------------------------------------------------------------------
 240          //
 241          // Return Value:  None
C51 COMPILER V8.16   F32X_ADC0_EXTERNALINPUT                                               11/12/2008 20:36:32 PAGE 5   

 242          // Parameters:    None
 243          //
 244          // Configures ADC0 to make single-ended analog measurements on pin P2.4
 245          //  
 246          //-----------------------------------------------------------------------------
 247          
 248          void ADC0_Init (void)
 249          {
 250   1      
 251   1         ADC0CN = 0x02;                      // ADC0 disabled, normal tracking, 
 252   1                                             // conversion triggered on TMR2 overflow
 253   1      
 254   1         REF0CN = 0x03;                      // Enable on-chip VREF and buffer
 255   1      
 256   1         AMX0P = 0x0C;                       // ADC0 positive input = P2.4
 257   1         AMX0N = 0x1F;                       // ADC0 negative input = GND
 258   1                                             // i.e., single ended mode
 259   1      
 260   1         ADC0CF = ((SYSCLK/3000000)-1)<<3;   // set SAR clock to 3MHz
 261   1      
 262   1         ADC0CF |= 0x00;                     // right-justify results 
 263   1      
 264   1         EIE1 |= 0x08;                       // enable ADC0 conversion complete int.
 265   1      
 266   1         AD0EN = 1;                          // enable ADC0
 267   1      }
 268          
 269          //-----------------------------------------------------------------------------
 270          // UART0_Init
 271          //-----------------------------------------------------------------------------
 272          //
 273          // Return Value:  None
 274          // Parameters:    None
 275          //
 276          // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
 277          //
 278          //-----------------------------------------------------------------------------
 279          
 280          void UART0_Init (void)
 281          {
 282   1         SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
 283   1                                             //        level of STOP bit is ignored
 284   1                                             //        RX enabled
 285   1                                             //        ninth bits are zeros
 286   1                                             //        clear RI0 and TI0 bits
 287   1         if (SYSCLK/BAUDRATE/2/256 < 1) {
 288   2            TH1 = -(SYSCLK/BAUDRATE/2);
 289   2            CKCON |=  0x08;                  // T1M = 1; SCA1:0 = xx
 290   2         } else if (SYSCLK/BAUDRATE/2/256 < 4) {
 291   2            TH1 = -(SYSCLK/BAUDRATE/2/4);
 292   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
 293   2            CKCON |=  0x01;
 294   2         } else if (SYSCLK/BAUDRATE/2/256 < 12) {
 295   2            TH1 = -(SYSCLK/BAUDRATE/2/12);
 296   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
 297   2         } else if (SYSCLK/BAUDRATE/2/256 < 48) {
 298   2            TH1 = -(SYSCLK/BAUDRATE/2/48);
 299   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
 300   2            CKCON |=  0x02;
 301   2         } else {
 302   2            while (1);                       // Error.  Unsupported baud rate
 303   2         }
C51 COMPILER V8.16   F32X_ADC0_EXTERNALINPUT                                               11/12/2008 20:36:32 PAGE 6   

 304   1      
 305   1         TL1 = TH1;                          // init Timer1
 306   1         TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
 307   1         TMOD |=  0x20;
 308   1         TR1 = 1;                            // START Timer1
 309   1         TI0 = 1;                            // Indicate TX0 ready
 310   1      }
 311          
 312          //-----------------------------------------------------------------------------
 313          // Interrupt Service Routines
 314          //-----------------------------------------------------------------------------
 315          
 316          //-----------------------------------------------------------------------------
 317          // ADC0_ISR
 318          //-----------------------------------------------------------------------------
 319          // 
 320          // This ISR averages 2048 samples then prints the result to the terminal.  The 
 321          // ISR is called after each ADC conversion which is triggered by Timer2.
 322          //
 323          //-----------------------------------------------------------------------------
 324          void ADC0_ISR (void) interrupt 10
 325          {
 326   1      
 327   1         static unsigned long accumulator = 0;     // accumulator for averaging
 328   1         static unsigned int measurements = 2048;  // measurement counter
 329   1         unsigned long result=0;
 330   1         unsigned long mV;                         // measured voltage in mV
 331   1      
 332   1         AD0INT = 0;                               // clear ADC0 conv. complete flag
 333   1      
 334   1         accumulator += ADC0;
 335   1         measurements--;
 336   1      
 337   1         if(measurements == 0)
 338   1         {  
 339   2            measurements = 2048;
 340   2            result = accumulator / 2048;
 341   2            accumulator=0;
 342   2      
 343   2            // The 10-bit ADC value is averaged across 2048 measurements.  
 344   2            // The measured voltage applied to P1.4 is then:
 345   2            //
 346   2            //                           Vref (mV)
 347   2            //   measurement (mV) =   --------------- * result (bits) 
 348   2            //                       (2^10)-1 (bits)
 349   2      
 350   2            mV =  result * 2440 / 1023;   
 351   2            printf("P2.4 voltage: %ld mV\n",mV);
 352   2         }
 353   1      
 354   1         LED = ~LED;
 355   1      }
 356          
 357          //-----------------------------------------------------------------------------
 358          // End Of File
 359          //-----------------------------------------------------------------------------


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    309    ----
   CONSTANT SIZE    =     22    ----
   XDATA SIZE       =   ----    ----
C51 COMPILER V8.16   F32X_ADC0_EXTERNALINPUT                                               11/12/2008 20:36:32 PAGE 7   

   PDATA SIZE       =   ----    ----
   DATA SIZE        =      6       4
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -