Pages

2011-04-27

Microchip PIC: Using TIMER0

In this memo Timer 0 is operated in 8-bit mode. The time for an interrupt is given by:


where Prescaler is the selected prescaler value, and TMR0L is the value loaded into timer register TMR0L to generate timer interrupts every Time period.

In our application the clock frequency is 4MHz, that is, clock period = 0.25µs, and Time = 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0Lcan be calculated as follows:
Thus, TMR0L should be loaded with 100. The value to be loaded into TMR0 control register T0CON can then be found as:
Thus, T0CON register should be loaded with hexadecimal 0xC4. The next register to be configured is the interrupt control register INTCON, where we will disable priority based interrupts and enable the global interrupts and TMR0 interrupts:
Taking the don’t-care entries (X) as 0, the hexadecimal value to be loaded intoregister INTCON is 0xA0.

When an interrupt occurs, the program automatically jumps to the interrupt service routine. Inside this routine we have to reload register TMR0L, reenable the TMR0 interrupts, and clear the TMR0 interrupt flag bit. Setting INTCON register to 0x20 reenables the TMR0 interrupts and at the same time clears the TMR0 interrupt flag.

The operations to be performed can thus be summarized as follows:
In the main program:
  • Load TMR0L with 100
  • Set T0CON to 0xC4
  • Set INTCON to 0xA0
  • Increment the counter with 1-second delays
In the interrupt service routine:
  • Re-load TMR0L to 100
  • Refresh displays
  • Set INTCON to 0x20 (reenable TMR0 interrupts and clear timer interrupt flag)
Source code example:

      2011-04-08

      DotNet: Using Sockets in C#

      • Using TcpListener for creating a TCP server:
      using System;
      using System.Threading;
      using System.Net;
      using System.Net.Sockets;
      using System.IO;
      private Socket connection; // Socket for accepting a connection
      private Thread readThread; // Thread for processing incoming messages
      private NetworkStream socketStream; // network data stream
      private BinaryWriter writer; // facilitates writing to the stream
      private BinaryReader reader; // facilitates reading from the stream
      readThread = new Thread( new ThreadStart( RunServer ) );
      readThread.Start();
      public void RunServer()
      {

      TcpListener listener; 
      // Step 1: create TcpListenerIPAddress local = IPAddress.Parse( "127.0.0.1" );listener = new TcpListener( local, 50000 ); 
      // Step 2: TcpListener waits for connection requestlistener.Start(); 
      // Step 3: establish connection upon client requestwhile ( true ){
      // accept an incoming connectionconnection = listener.AcceptSocket(); 
      // create NetworkStream object associated with socketsocketStream = new NetworkStream( connection ); 
      // create objects for transferring data across streamwriter = new BinaryWriter( socketStream );reader = new BinaryReader( socketStream ); 
      // Step 4: read string data sent from client 

      // Step 5: close connectionwriter.Close();reader.Close();socketStream.Close();connection.Close();
      }
      }