What motor driver are you using? Which encoder?
Don't forget that the datasheet of the motor driver will tell you exactly what to do to control a motor
Oh and you'll need to use hardware interrupts if you use an encoder.
(we can't help you without more info)